Search with um_activity shared_link post_meta and show all matching post_meta results

Question

I am using Kleo-child theme. I have an active plugin Ultimate member social activity. I want to search for any keyword from um_shared_link post_meta and see the result that keyword activity what I search for. What exactly should I do for that? Here is my functions.php code:

function update_my_custom_type() {

global $wp_post_types;

if ( post_type_exists( 'um_activity' ) ) {

    // exclude from search results
    $wp_post_types['um_activity']->exclude_from_search = false;
  }
}

 add_action( 'wp_enqueue_scripts', 'enqueue_parent_styles' );

function enqueue_parent_styles() {

wp_enqueue_style( 'parent-style', 

get_template_directory_uri().'/style.css' );

 }

add_action( 'wp_ajax_kleo_ajax_search', 'kleo_ajax_search' );

add_action( 'wp_ajax_nopriv_kleo_ajax_search', 'kleo_ajax_search' );

if ( ! function_exists( 'kleo_ajax_search' ) ) {

function kleo_ajax_search() {

//if "s" input is missing exit

if ( empty( $_REQUEST['s'] ) && empty( $_REQUEST['bbp_search'] ) ) 
{
  die();
}

if ( ! empty( $_REQUEST['bbp_search'] ) ) {

  $search_string = $_REQUEST['bbp_search'];

} else {

  $search_string = $_REQUEST['s'];
}

$output   = '';
$context  = 'any';
$defaults = array(
  'numberposts'      => 4,
  'posts_per_page'   => 20,
  'post_type'        => 'any',
  'post_status'      =>  array('publish','inherit'),
  'post_password'    => '',
  'suppress_filters' => false,
  'meta_query' => array(
    array(
       'key' => '_shared_link',
       'value' => $_REQUEST['s'],
       'compare' => 'LIKE'
    )
 )
);

if ( empty( $defaults['post_type'] ) ) {
  $posts = null;
} else {

  $defaults = apply_filters( 'kleo_ajax_query_args', $defaults );

  $the_query = new WP_Query( $defaults );
  $posts     = $the_query->get_posts();
//  echo "the query is".$the_query->request;
  //die();
  /*$the_query_meta = new WP_Query($defaults_meta);
  $posts_meta = $the_query->get_posts();
  $posts = array_merge($posts,$posts_meta);*/
}

$members          = array();
$members['total'] = 0;
$groups           = array();
$groups['total']  = 0;
$forums           = false;


if ( function_exists( 'bp_is_active' ) && ( $context == "any" || 
in_array( "members", $context ) ) ) {
  $members = bp_core_get_users( array(
    'search_terms'    => $search_string,
    'per_page'        => $defaults['numberposts'],
    'populate_extras' => false,
  ) );
}

if ( function_exists( 'bp_is_active' ) && bp_is_active( "groups" ) && 
( $context == "any" || in_array( "groups", $context ) ) ) {
  $groups = groups_get_groups( array(
    'search_terms'    => $search_string,
    'per_page'        => $defaults['numberposts'],
    'populate_extras' => false,
  ) );
}

if ( class_exists( 'bbPress' ) && ( $context == "any" || in_array( 
"forum", $context ) ) ) {
  $forums = kleo_bbp_get_replies( $search_string );
}


//if there are no posts, groups nor members
if ( empty( $posts ) && $members['total'] == 0 && $groups['total'] == 
0 && ! $forums ) {
  $output = "<div class='kleo_ajax_entry ajax_not_found'>";
  $output .= "<div class='ajax_search_content'>";
  $output .= "<i class='icon icon-attention-circled'></i> ";
  $output .= __( "Sorry, we haven't found anything based on your 
  criteria.", 'kleo_framework' );
  $output .= "<br>";
  $output .= __( "Please try searching by different terms.", 
  'kleo_framework' );
  $output .= "</div>";
  $output .= "</div>";
  echo $output;
  die();
}

//if there are members
if ( $members['total'] != 0 ) {

  $output .= '<div class="kleo-ajax-part kleo-ajax-type-members">';
  $output .= '<h4><span>' . __( "Members", 'kleo_framework' ) . 
 '</span></h4>';
  foreach ( (array) $members['users'] as $member ) {
    $image = '<img src="' . bp_core_fetch_avatar( array(
        'item_id' => $member->ID,
        'width'   => 25,
        'height'  => 25,
        'html'    => false
      ) ) . '" class="kleo-rounded" alt="">';
    if ( $update = bp_get_user_meta( $member->ID, 'bp_latest_update', true ) ) {
      $latest_activity = char_trim( trim( strip_tags( bp_create_excerpt( $update['content'], 50, "..." ) ) ) );
    } else {
      $latest_activity = '';
    }
    $output .= "<div class ='kleo_ajax_entry'>";
    $output .= "<div class='ajax_search_image'>$image</div>";
    $output .= "<div class='ajax_search_content'>";
    $output .= "<a href='" . bp_core_get_user_domain( $member->ID ) . "' class='search_title'>";
    $output .= $member->display_name;
    $output .= "</a>";
    $output .= "<span class='search_excerpt'>";
    $output .= $latest_activity;
    $output .= "</span>";
    $output .= "</div>";
    $output .= "</div>";
  }
  $output .= "<a class='ajax_view_all' href='" . esc_url( bp_get_members_directory_permalink() . "?s=" . $search_string ) . "'>" . __( 'View member results', 'kleo_framework' ) . "</a>";
  $output .= "</div>";
}

//if there are groups
if ( $groups['total'] != 0 ) {

  $output .= '<div class="kleo-ajax-part kleo-ajax-type-groups">';
  $output .= '<h4><span>' . __( "Groups", 'kleo_framework' ) . '</span></h4>';
  foreach ( (array) $groups['groups'] as $group ) {
    $image = '<img src="' . bp_core_fetch_avatar( array(
        'item_id' => $group->id,
        'object'  => 'group',
        'width'   => 25,
        'height'  => 25,
        'html'    => false
      ) ) . '" class="kleo-rounded" alt="">';
    $output .= "<div class ='kleo_ajax_entry'>";
    $output .= "<div class='ajax_search_image'>$image</div>";
    $output .= "<div class='ajax_search_content'>";
    $output .= "<a href='" . bp_get_group_permalink( $group ) . "' class='search_title'>";
    $output .= $group->name;
    $output .= "</a>";
    $output .= "</div>";
    $output .= "</div>";
  }
  $output .= "<a class='ajax_view_all' href='" . esc_url( bp_get_groups_directory_permalink() . "?s=" . $search_string ) . "'>" . __( 'View group results', 'kleo_framework' ) . "</a>";
  $output .= "</div>";
}

//if there are posts
if ( ! empty( $posts ) ) {
  $post_type_str = array();
  $post_types    = array();
  $post_type_obj = array();
  foreach ( $posts as $post ) {
    $post_types[ $post->post_type ][] = $post;
    if ( empty( $post_type_obj[ $post->post_type ] ) ) {
      $post_type_obj[ $post->post_type ] = get_post_type_object( $post->post_type );
    }
  }

  foreach ( $post_types as $ptype => $post_type ) {
    $output .= '<div class="kleo-ajax-part kleo-ajax-type-' . esc_attr( $post_type_obj[ $ptype ]->name ) . '">';
    if ( isset( $post_type_obj[ $ptype ]->labels->name ) ) {
      $output .= "<h4><span>" . $post_type_obj[ $ptype ]->labels->name . "</span></h4>";
    } else {
      $output .= "<hr>";
    }
    $count = 0;
    foreach ( $post_type as $post ) {

      $post_type_str[$post->post_type] = $post->post_type;
      $count ++;
      if ( $count > 4 ) {
        continue;
      }
      $format = get_post_format( $post->ID );
                if( $post->post_type == 'attachment') {
                    $img_url = wp_get_attachment_thumb_url( $post->ID );
                    $image = '<img src="'.aq_resize( $img_url, 44, 44, true, true, true ).'" class="kleo-rounded"/>';
                } else {
                    if ($img_url = kleo_get_post_thumbnail_url($post->ID)) {
                        $image = aq_resize($img_url, 44, 44, true, true, true);
                        if (!$image) {
                            $image = $img_url;
                        }
                        $image = '<img src="' . $image . '" class="kleo-rounded">';
                    } else {
                        if ($format == 'video') {
                            $image = "<i class='icon icon-video'></i>";
                        } elseif ($format == 'image' || $format == 'gallery') {
                            $image = "<i class='icon icon-picture'></i>";
                        } else {
                            $image = "<i class='icon icon-link'></i>";
                        }
                    }
                }
      $excerpt = "";

      if ( ! empty( $post->post_content ) ) {
        $excerpt = $post->post_content;
        $excerpt = preg_replace( "/[(.*?)(?:(/))?](?:(.+?)[/2])?/s", '', $excerpt );
        $excerpt = wp_strip_all_tags($excerpt); //added to remove gogole adsense code from search excerpt
        $excerpt = char_trim( trim( strip_tags( $excerpt ) ), 40, "..." );
      }
      $link    = 'activity/?wall_post='.$post->ID;
      $result  = $post->ID;
      $classes = "format-" . $format;
      $output .= "<div class ='kleo_ajax_entry $classes'>";
      $output .= "<div class='ajax_search_image'>$image</div>";
      $output .= "<div class='ajax_search_content'>";
      $output .= "<a href='$link' class='search_title'>$result";
      $output .= $excerpt;
      $output .= "</a>";
      $output .= "<span class='search_excerpt'>";
      $output .= $excerpt;
      $output .= "</span>";
      $output .= "</div>";
      $output .= "</div>";
    }
    $output .= '</div>';
  }

  if ( ! empty( $post_type_str ) ) {
    if ( count( $post_type_str ) > 1 ) {
      $search_str_posts = '&post_type[]=' . implode( ',', $post_type_str );
    } else {
      $search_str_posts = '&post_type=' . implode( ',', $post_type_str );
    }

  } else {
    $search_str_posts = '';
  }

  $output .= "<a class='ajax_view_all' href='" . esc_url( home_url( '/' ) . '?s=' . $search_string ) . $search_str_posts . "'>" . __( 'View all results', 'kleo_framework' ) . "</a>";
}

/* Forums topics search */
if ( ! empty( $forums ) ) {
  $output .= '<div class="kleo-ajax-part kleo-ajax-type-forums">';
  $output .= '<h4><span>' . __( "Forums", 'kleo_framework' ) . '</span></h4>';

  $i = 0;
  foreach ( $forums as $fk => $forum ) {

    $i ++;
    if ( $i <= 4 ) {
      $image = "<i class='icon icon-chat-1'></i>";

      $output .= "<div class ='kleo_ajax_entry'>";
      $output .= "<div class='ajax_search_image'>$image</div>";
      $output .= "<div class='ajax_search_content'>";
      $output .= "<a href='" . $forum['url'] . "' class='search_title'>";
      $output .= $forum['name'];
      $output .= "</a>";
      //$output .= "<span class='search_excerpt'>";
      //$output .= $latest_activity;
      //$output .= "</span>";
      $output .= "</div>";
      $output .= "</div>";
    }
  }
  $output .= "<a class='ajax_view_all' href='" . esc_url( bbp_get_search_url() . "?bbp_search=" . $search_string ) . "'>" . __( 'View forum results', 'kleo_framework' ) . "</a>";
  $output .= "</div>";
}


echo $output;
die();
 }
 }

function searchfilter($query) {

if ($query->is_search) {
    $query->set('post_type',array('um_activity'));
}

return $query;
}

add_filter('pre_get_posts','searchfilter');
0
, Soumodip 4 years 2019-11-02T01:36:48-05:00 0 Answers 74 views 0

Leave an answer

Browse
Browse