简体   繁体   中英

Add custom posts to search (separately to categories) in Wordpress

I have a search results page under construction, and it splits the posts out in to categories (products, recipes, articles), in 3 separate sections down the page. The categories (recipes and articles) were fine and easy - creating a query to use in 2 separate loops, but I'm having trouble with the custom post type.

I want to pull these in with the loop as well if possible, but not sure whether to do it on the post type or taxonomy. The products are obviously split in to sub categories under the taxonomy, so when I tried it, it was pulling the same product multiple times.

Right now, having given up on trying to pull them through using the taxonomy, I am trying this:

<?php
    $args = array(
        'post_type'=> 'products'
    );
    $products = new WP_Query($args);
    if ( have_posts() ) :
?>

        <div class="product suggestions cfx sub-range">
            <h2>We found xx products...</h2>

<?php while ( $products->have_posts() ) : $products->the_post(); ?>

This code isn't pulling anything through...will a different query work, or should I still be trying to use the custom taxonomy?

When I search for a solution, pretty much everything I can find is creating a search.php page for displaying only custom post types, or just adding the custom post type to search results, not having a separate loop for custom post types along side the loops for categories...

Thanks

I think I got it. I changed the slug from products and it started working for some reason. So now i have this:

<?php
    $args = array ('post_type'=>'product-ranges');
    $products = new WP_Query($args);
    if ( have_posts() ) :
?>

        <div class="product suggestions cfx sub-range">
            <h2>We found xx products...</h2>

<?php while ( $products->have_posts() ) : $products->the_post();
 ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM