0
 add_action( 'pre_get_posts' , 'my_pre_get_posts' );
    Function my_pre_get_posts( $query ) {
        $value= $_GET['s'];
        if( $query->is_main_query() && $query->is_search()) {
            $query->set( 
              'meta_query',
              array( 
                array(
                  'key' => 'pdf_content',
                  'value' => $value,
                  'compare' => 'LIKE'
                )  
              ) 
            ); 
         }
    }

I am not getting any result from this query please help me?

WP_Query Object ( [query] => Array ( [s] => construction [post_type] => iep_section ) [query_vars] => Array ( [s] => construction [post_type] => iep_section [error] => [m] => [p] => 0 [post_parent] => [subpost] => [subpost_id] => [attachment] => [attachment_id] => 0 [name] => [pagename] => [page_id] => 0 [second] => [minute] => [hour] => [day] => 0 [monthnum] => 0 [year] => 0 [w] => 0 [category_name] => [tag] => [cat] => [tag_id] => [author] => [author_name] => [feed] => [tb] => [paged] => 0 [meta_key] => [meta_value] => [preview] => [sentence] => [title] => [fields] => [menu_order] => [embed] => [category__in] => Array ( ) [category__not_in] => Array ( ) [category__and] => Array ( ) [post__in] => Array ( ) [post__not_in] => Array ( ) [post_name__in] => Array ( ) [tag__in] => Array ( ) [tag__not_in] => Array ( ) [tag__and] => Array ( ) [tag_slug__in] => Array ( ) [tag_slug__and] => Array ( ) [post_parent__in] => Array ( ) [post_parent__not_in] => Array ( ) [author__in] => Array ( ) [author__not_in] => Array ( ) [meta_query] => Array ( [0] => Array ( [key] => pdf_content [compare] => LIKE [value] => construction [type] => char ) ) ) [tax_query] => WP_Tax_Query Object ( [queries] => Array ( ) [relation] => AND [table_aliases:protected] => Array ( ) [queried_terms] => Array ( ) [primary_table] => [primary_id_column] => ) [meta_query] => [date_query] => [post_count] => 0 [current_post] => -1 [in_the_loop] => [comment_count] => 0 [current_comment] => -1 [found_posts] => 0 [max_num_pages] => 0 [max_num_comment_pages] => 0 [is_single] => [is_preview] => [is_page] => [is_archive] => 1 [is_date] => [is_year] => [is_month] => [is_day] => [is_time] => [is_author] => [is_category] => [is_tag] => [is_tax] => [is_search] => 1 [is_feed] => [is_comment_feed] => [is_trackback] => [is_home] => [is_privacy_policy] => [is_404] => [is_embed] => [is_paged] => [is_admin] => [is_attachment] => [is_singular] => [is_robots] => [is_posts_page] => [is_post_type_archive] => 1 [query_vars_hash:WP_Query:private] => fbc82700aadf968ad3d001310266de18 [query_vars_changed:WP_Query:private] => [thumbnails_cached] => [stopwords:WP_Query:private] => [compat_fields:WP_Query:private] => Array ( [0] => query_vars_hash [1] => query_vars_changed ) [compat_methods:WP_Query:private] => Array ( [0] => init_query_flags [1] => parse_tax_query ) )

6
  • 1
    Can you please help me here? Commented Oct 18, 2019 at 11:24
  • 1
    Still not working Commented Oct 18, 2019 at 11:42
  • No, I tried your query but still not getting any results? Commented Oct 18, 2019 at 11:44
  • This is the print query on front page Commented Oct 18, 2019 at 11:47
  • The thing is.. your query would search for posts having for example construction in the post title, excerpt or content and that the posts have the meta pdf_content set to construction. Are there posts having such criteria?
    – Sally CJ
    Commented Oct 18, 2019 at 15:19

2 Answers 2

1

When using LIKE clause, you should be using % to the start and end of the value to make it match with the existing values. So your query should look like this.

add_action( 'pre_get_posts' , 'my_pre_get_posts' );
function my_pre_get_posts( $query ) {
    $value= '%'.$_GET['s'].'%';
    if( $query->is_main_query() && $query->is_search() ) {
        $query->set( 
          'meta_query',
          array( 
            array(
              'key' => 'pdf_content',
              'value' => $value,
              'compare' => 'LIKE'
            )  
          ) 
        ); 
     }
}
3
  • still not working sir Commented Oct 18, 2019 at 13:54
  • Make sure you have valid data in DB that should be returned, data has all the required specs like post type, status etc. Morover, check if $query->is_search() is returning true.
    – M-R
    Commented Oct 18, 2019 at 14:02
  • May be it's not the main query but a custom one. When there's the 's' in the request is_search it's generally true. Btw, I agree the compare clause may not be match the value.
    – guido
    Commented Oct 18, 2019 at 21:11
0

I fixed the problem by adding this code in search query

$args = array(
    'post_type'=> 'iep_section',

    'order'    => 'ASC',
    'meta_query' => array(
        array(
            'key' => 'pdf_content',
            'value' => $value,

            'compare' => 'LIKE'
        )
    )
);

query_posts( $args );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.