0

I have a custom post type 'property'. I am trying to make a search for it, but it does not work with wp_query parameter 's'.

$wp_query = Wp_Query(['post_type' => 'property', 's' => 'test']);

It works fine with other Wp_query parameters, like this:

$wp_query = Wp_Query([
    'post_type' => 'property',
    [
        'taxonomy' => 'property_usage_type',
        'field' => 'id',
        'terms' => $_GET['sb-usage-type'],
    ]
]);

It also works with other parameters except for 's'. But 's' is working with 'post' post type. I also tried to echo out sql query - $wp_query->request but it is echos out anything unless I remove 's'.

  • 's' is just searching for the title & content but not for custom fields inside the custom post type. Are you searching for content in the custom post-type or are you searching for content of a custom field? – marvinpoo May 14 '18 at 12:23
  • yes know that 's' is for title and content and I need to search title and content of custom post type. that custom field i inserted to show that wp query is working with other parameters except for 's' – Tomas Cerniauskas May 14 '18 at 12:26
  • Can you update your question and show the form you are using? – marvinpoo May 14 '18 at 12:28
  • Here is what I use for custom post types searches: $argsXXX = array( 'posts_per_page' => 10, 'post_type' => 'custom_type' ); And then $loopXXX = new WP_Query( $argsXXX ); while ( $loopXXX->have_posts() ) : $loopXXX->the_post(); – marvinpoo May 14 '18 at 12:32
  • I dont understand how do you search in custom post types. You just loop through all custom post type. Anyway, I dont think posting a form itself would help. Because it doesnt work even though I write a keyword directly in a code as I posted before $wp_query = Wp_Query(['post_type' => 'property', 's' => 'test']); and work with 'post' post type. Generally what i need is that i want to get all 'property' custom post types, that contain a keyword 'test' in the title or content. – Tomas Cerniauskas May 14 '18 at 12:42
1

I tried to reproduce the problem and you are right I had the same issue with s parameter + post_type filter.

I suppose that Wordpress do this for any reason.

You can fix this adding this in your template or in your functions.php

add_filter( 'pre_get_posts', 'tgm_io_cpt_search' );
/**
 * This function modifies the main WordPress query to include an array of
 * post types instead of the default 'post' post type.
 *
 * @param object $query  The original query.
 * @return object $query The amended query.
 */
function tgm_io_cpt_search( $query ) {

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

    return $query;

}

$the_query = new WP_Query( array('s' => 'test') );

Reference code: https://thomasgriffin.io/how-to-include-custom-post-types-in-wordpress-search-results/

  • Welcome to the WP Stackexchange. While this answer is pretty correct, it is only working if you are not working with custom meta in your custom search, if the user is able to set their own filters. While giving an answer, try to maintain a scalable solution for further development. – marvinpoo May 14 '18 at 14:14
  • you are the lifesaver, thanks. It works with other custom fields too – Tomas Cerniauskas May 14 '18 at 14:34
  • ;) happy to help! – IvanMunoz May 14 '18 at 14:36
  • 1
    @marvinpoo I tried to use meta_key in the query and it's working as usual. Regards! – IvanMunoz May 14 '18 at 14:37
  • I am leaving work right now, but I will try it when I am back at work tomorrow. But I've tried this with some very specific customization and with conditional searching based on post type and meta key value. I will try it out nontheless and will report back to you ;) – marvinpoo May 14 '18 at 15:15
0

You can do like this,

$args = array(
      'post_type' => 'tribe_events', 
      'post_per_page' =>  get_option('posts_per_page'), 
       's' => get_search_query()
   );

$query = new WP_Query($args); // Use new keyword and you need to use WP_Query not Wp_Query

while($query->have_posts()): $query->the_post();

     the_title();

endwhile;wp_reset_postdata(); 

It used this in search.php file. Worked without any problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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