• $query->set('post_type', array("cpt", "post"));
    add_filter('pre_get_posts', 'search_filter');

    Hi, I set the post types with pre_get_posts filter as mentioned above.

    Now there are three circumstances:

    1) I search without terms: ?s= this will get only the wp core posts, ‘cpt’ custom post type is not showing.

    2) I search with terms: ?s=search+term this will get me both ‘cpt’ and wp core posts.

    3) I search with: post_type=cpt this will get all the ‘cpt’ posts.

    Is there any way to find both ‘cpt’ and wp core posts in the point 1 like in the point 2?

    Thanks!

    • This topic was modified 4 years, 7 months ago by elfigos.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Mikko Saari

    (@msaari)

    Does point 1 work better, if you use

    add_filter( 'relevanssi_modify_wp_query', function( $query ) {
      $query->set( 'post_types', array( 'cpt', 'post' ) );
      return $query;
    } );

    instead?

    Thread Starter elfigos

    (@elfigos)

    Nope, if I use your code, even when I use post_type=cpt in the url I will get only wp core posts.

    Plugin Author Mikko Saari

    (@msaari)

    I’d say the termless search is not going through Relevanssi at all in that case. Can you please provide some more context? It’s really impossible for me to say anything with this little detail.

    Thread Starter elfigos

    (@elfigos)

    MMhg, I noticed a strange thing.

    The termless search is getting all the wp core posts + the posts(custom post type) that I converted from wp core post to the custom post with a custom post type switcher plugin. But cannot retrieve the custom posts that I natively created.

    Thread Starter elfigos

    (@elfigos)

    By the way, how do I stop wordpress/relevanssi from searching (from querying and not from showing) if no terms are entered?

    Plugin Author Mikko Saari

    (@msaari)

    add_filter( 'relevanssi_search_ok', 'rlv_no_termless_search', 10, 2 );
    function rlv_no_termless_search( $ok, $query ) {
      if ( empty( $query->query_vars['s'] ) ) {
        $ok = false;
      }
      return $ok;
    }
Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Search all posts + custom post type posts without search term’ is closed to new replies.