0

I have created a custom post type named "my-products" and registered custom taxonomy "my-products-categories"

Then I created some Categories like "Webdesign", "Print", "Logo"

I need to display all the posts divided in their categories inside jQuery Tabs. So you could think about it like in the first Tab I have the posts from the Category "Webdesign" and possible sub-categories, in the second Tab all the posts from "Print" and possible sub-categories etc.

Here is my approach:

function displayCustomPosts() {
    $the_query = new WP_Query(array( 'post_type' => 'my-products')); ?>

    if ( $the_query->have_posts() ) :
        while ( $the_query->have_posts() ) : $the_query->the_post();

            jQuery Tabs with the posts

        endwhile;
    endif;
}

What my problem is is that I dont want to use multiple WP_Query to query the different categories and display them separately but to only use WP_Query once and filter the categories afterwards. That would be better for the performance I think!?

So is there a way to do that with WP_Query like first query all posts from every category and then when displaying the content of those posts filter them by their categories.

Or would I need to use multiple queries to achieve what I want?

1 Answer 1

0

I would recommend looping thru your query result ($the_query in your example) multiple times, pulling out the matching posts each time.

Use $the_query-rewind_posts() after each pass to restart your loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.