1
votes

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

1
if it is woocommerce the post_type should be product not products. That could be your issue hereMichael Doye
It's not woocommerce, it's a custom post type I've created. Thanks though.Forxs
Do you mean if ( $products->have_posts() )?user488187

1 Answers

0
votes

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();
 ?>