Home Forums Trusted Pro Search pop-up misbehavior… pull down does not work.

This topic contains 9 replies, has 2 voices, and was last updated by  Adri Oosterwijk 2 months, 3 weeks ago.

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #3696

    Adri Oosterwijk
    Participant

    Hi,

    I recently noticed that the search popup is not behaving the way it should.

    When hitting the pull down menu nothing is happening. Performing a search it seems that it is only searching the products custom type. I can’t select any other type or custom type.

    I have created a custom post type using WP-Types, a part of Toolset. Disabling the Toolset plugins (and therefor the custom post type?) is not solving it.

    I tried to download the YITH Woocommerce Ajax Search plugin but that did not help either.

    What should I do?

    Best wishes,

    Adri

    #3697

    Andy
    Keymaster

    The way the search works is if WooCommerce is not active it uses the standard WP get_search_form() function.

    If WooCommerce is active then it makes use of the theme’s custom search function – trusted_woocommerce_search_form() – to search WooCommerce products.

    The trusted_woocommerce_search_form() function can be overridden in a child theme. Look in extras.php line 817 to 841, copy the function to the child theme’s functions.php file and remove the part that displays the WooCommerce categories dropdown (line 823 to 833).

    Now the important part to change is line 835: <input type="hidden" name="post_type" value="product">
    where you should change ‘product’ to the name of your custom post type.

    #3701

    Adri Oosterwijk
    Participant

    Mmmm, I possibly misunderstand you. I did understand the dropdown had the purpose to let me select

    1 -> Products (Dutch: Producten)
    2-> Another custom post type (in my case Recepies -> Dutch: Recepten).
    3 -> Posts and Pages (Dutch: Berichten en Pagina’s)

    Browsing through the code I learned (like you said) that it will only list Product categories.

    The reason that I want it this way is that this will be a shop with a very limited amount of products. All uncategorized.
    The amount of Recepies however can be much more and will grow over time. Next to that would it be great in may case that the visitor can search the rest of the site as well.

    When I get this right it can keep my sidebars much cleaner. As it is now I have to add several search widgets and that makes the layout of the pages a bit messy for the visitors. The search option of the theme is great for this…..

    Any chance that it can be changed in an easy wat to get what I want?

    Best wishes,

    Adri

    #3702

    Andy
    Keymaster

    The dropdown allows you or your site visitors to search within a WooCommerce product category. It does not work for selecting within post types as you listed in 1, 2, 3 above.

    OK, a better way for you achieve what you want and let you filter the search to search within specific post types.
    Add this code to the child theme’s functions.php file (this overrides our WooCommerce search form with the standard WP search form)

    function trusted_woocommerce_search_form() {
        get_search_form();
    }

    Now let’s assume you have a custom post type named recipe, add this code to the child theme’s functions.php file

    function trusted_custom_search_filter( $query ) {
        if ( $query->is_search ) {
            $query->set( 'post_type', array( 'recipe' ) );
        }
        return $query;
    }
    add_filter( 'pre_get_posts', 'trusted_custom_search_filter' );

    this code filters the standard search to search within posts that are recipes (your custom post type).

    If you wanted to expand the search to also search within other post types, you would simply include more post types in the post_type array.
    For example, to search recipes, posts and products, the code would be like so:

    function trusted_custom_search_filter( $query ) {
        if ( $query->is_search ) {
            $query->set( 'post_type', array( 'recipe', 'post', 'product' ) );
        }
        return $query;
    }
    add_filter( 'pre_get_posts', 'trusted_custom_search_filter' );
    #3703

    Adri Oosterwijk
    Participant

    Hi Andy,

    Thank you for your quick reply and outstanding help.  I did what you suggested and now I can search in any post (recipes, products and other (default) posts and pages. I do have another wish however…. sorry :-). Is it possible to keep the pull down menu to select what to search?

    1 -> All
    2-> Products
    3-> Recipes
    4-> Posts and Pages

    I hope you can provide me with a simple solution…..

    Thanks in dvance.

    Best wishes,

    Adri

    #3704

    Andy
    Keymaster

    Sorry no simple solution.

    Just thinking out loud here, do any of the plugins you looked at let you select your 4 options?

    If so, and if the plugin has a widget (or shortcode) you could place it in the theme’s Top Bar widget area and use that instead.

    #3708

    Adri Oosterwijk
    Participant

    Hi Andy,

    The plugins I’ve looked at did not have this functionallity (as far as I know).

    For now, I’m happy the way it is.

    I will look into an option to precede the titles on the results page with: Product, Recipe or Post.
    I think it can be achieved when I customize the search result page with a Case statement:

    Case post type = product echo “Product” and the Title
    Case post type = recipe echo “Recipe” and the Title

    and so on.

    As I’m not a coder this will be a challenge …….

    Best wishes,

     

    Adri

    #3988

    Adri Oosterwijk
    Participant

    Hi Andy,

    I have almost accomplished what I what I need. In the image below you can see that the titles are labelled with the post type it belongs to. (“In de winkel” for Products, “In recepten” for recipes and so on)

    The issue I’m facing and I’m looking for a couple of days now is that sometimes a product has to be omitted from the search results. This is when the option “Hidden” is checked for Catalog visibility. See image below.

    The function you provided me (see above) works great, except for this little thingy.

    Would you be so kind to help me out with it?

    Thank you very much in advance.

    Best regards,

    Adri

    #3990

    Andy
    Keymaster

    You just need to add this to exclude products where the ‘product_visibility’ taxonomy is set to ‘exclude-from-search’:

    $query->set( 'tax_query', array(
        array(
        'taxonomy' => 'product_visibility',
        'field'    => 'name',
        'terms'    => 'exclude-from-search',
        'operator' => 'NOT IN',
        )
    ) );

    So the full code using the previous example would be:

    function trusted_custom_search_filter( $query ) {
        if ( $query->is_search ) {
            $query->set( 'post_type', array( 'recipe', 'post', 'product' ) );
            $query->set( 'tax_query', array(
                array(
                'taxonomy' => 'product_visibility',
                'field'    => 'name',
                'terms'    => 'exclude-from-search',
                'operator' => 'NOT IN',
                )
            ) );
        }
        return $query;
    }
    add_filter( 'pre_get_posts', 'trusted_custom_search_filter' );
    #3991

    Adri Oosterwijk
    Participant

    This works like a charm. Thank you very much. You rock! I will mark it solved now.

    Best regards,

    Adri

Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.