Support » Fixing WordPress » Post Tagging Problems

  • Resolved Pankaj Mohale



    I have facing issue because of post tagging in search form.
    In my site I have three type of search.
    1. Global Search – Search from all site.
    2. Only particular custom post type search (Books) – Search data only from “Book” custom post type.
    3. Product Search – Only from product. (I have used product search weight for that)

    Now my problem is:
    If I search from Custom post type search(book) or from product search(Woo-commerce widget) then it return me all data including blog post(WordPress default post type post).
    When I rectifying this issue it because of post tagging.

    Means if I add “book blog” tag to my “first blog post” and if I add “book CPT” tag to my custom post type post and then I search “book” in my custom post search then it should return “book CPT” only but instead of this it returns me both post.

    Why this happens I don’t know? Please suggest me what should I do?

    for custom post type search I have created Custom Search form using this link.
    <input type="hidden" name="post_type" value="book" />


Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz


    For unknown reasons, search queries are being changed to always search all post types no matter what you do on the form. Probably your theme or one of your plugins is doing this. The solution is to set the correct post type after other code has made its changes, thus your code has the final say. This is done through the “pre_get_posts” action. The following code added to your theme’s functions.php should address the book search issue.

    add_action('pre_get_posts', function( $query ) {
      if ( $query->is_search() && 'book' == $__POST['post_type']) {
        $query->set('post_type', 'book');
    }, 999 );

    The key is that 999, which should cause this code to run last. You can add a similar if statement for products. Just find something unique about the product search that can be checked, just like I did with $__POST['post_type'].

    Thread Starter Pankaj Mohale


    Thanks for replay.

    I have found the solution for this. Theme add “INNER JOIN” query in search so that why this is happens with me.

    Something like this:

    add_filter('posts_join', 'atom_search_join');
    add_filter('posts_groupby', 'atom_search_groupby'); 

    This used in theme for Candidate search. Which is not module so I commented this code.

    Thank you vary much for help.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Post Tagging Problems’ is closed to new replies.