[Resolved] Customizing Search Results – CPT's / Products

Home Forums Support [Resolved] Customizing Search Results – CPT's / Products

Home Forums Support Customizing Search Results – CPT's / Products

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #410762
    Brett

    Heya!

    I have a few sites on the go at the moment and they all require specific search results to be displayed.

    One site requires only WooCommerce products to show in the search results.

    Another site requires a selection of CPT’s (with all posts, pages, products hidden).

    Can I do this with some PHP?

    I guess I’m looking for info on how to add or hide post types from search results.

    Cheers,

    Brett

    #410851
    Leo
    Staff
    Customer Support
    #1389216
    Giacomo

    Hi Leo,
    i see the page you linekd, but i don’t understand how i can haide product of woocommerce from search results.
    Can help me?

    #1390230
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    This should help: https://wordpress.stackexchange.com/a/231376/90661

    To specify multiple post types, do this:

    add_action( 'pre_get_posts', function( $query ) {
      if( ! is_admin() && is_search() && $query->is_main_query() ) {
        $query->set( 'post_type', array( 'page', 'post' ) );
      }
    } );
    #1390672
    Giacomo

    Thanks Tom,
    works perfect ๐Ÿ˜€

    #1391696
    Tom
    Lead Developer
    Lead Developer

    You’re welcome ๐Ÿ™‚

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.