最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - wp_query 's' parameter does not work with custom post type

programmeradmin0浏览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'.

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'.

Share Improve this question asked May 14, 2018 at 12:18 Tomas CerniauskasTomas Cerniauskas 111 silver badge2 bronze badges 10
  • '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 Commented May 14, 2018 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 Commented May 14, 2018 at 12:26
  • Can you update your question and show the form you are using? – marvinpoo Commented May 14, 2018 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 Commented May 14, 2018 at 12:32
  • 1 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 Commented May 14, 2018 at 12:42
 |  Show 5 more comments

2 Answers 2

Reset to default 3

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(); 

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

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/

发布评论

评论列表(0)

  1. 暂无评论