Navigation
This is archived content. Visit our new forum.

Tagged: 

  • Author
    Posts
  • #1323890
    Tim
    Participant

    Hi,

    Is there a simple solution to show only woocommerce products on the search results?

    I have this piece of code:

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

    Which is working fine, but it also affects the backend search results..maybe you have an idea of modifying this to apply only in the frontend?

    Thanks..

    Cheers
    Tim

    #1323932
    Christian
    Moderator

    Hey Tim,

    Regretfully, this is outside the scope of our support. Please try using a search plugin like SearchWP. There are also other third party plugins for WooCommerce that can do that. Or, you might want to contact our trusted partners who caters X setup and customization needs. Please see https://community.theme.co/custom-development/

    Thank you for understanding.

    #1324029
    Tim
    Participant

    Hi Christian,

    Is this a new policy? In other threads you help customers with this issue far more deeply (back in 2014 & 15) but none of these answers seems to work anymore..

    #1324109
    Thai
    Moderator

    Hi There,

    Please update your code to this:

    add_filter( 'pre_get_posts', 'custom_search' );
    function custom_search( $query ) {
        if ( $query->is_search && !is_admin() ) {
            $query->set( 'post_type', array( 'product' ) );
        }
        return $query;
    } 

    Hope it helps 🙂

    #1336489
    Tim
    Participant

    Hi Thai,

    Works perfectly, thanks!

    Have a great day..

    Cheers

    #1336582
    Paul R
    Moderator

    You’re welcome! 🙂

  • <script> jQuery(function($){ $("#no-reply-1323890 .bbp-template-notice, .bbp-no-topic .bbp-template-notice").removeClass('bbp-template-notice'); }); </script>