0

I have my posts using this code :

$dataQuery = [
    'numberposts'   => -1,
    'post_type'     => 'documentation',
    'meta_query'    => $meta_query,
];

$documentations = get_posts($dataQuery);

Where $meta_query is an array. It's working, I have my posts, but I want to show a tree with the categories and the posts inside ? Is it possible easily ?

For now, I will get my categories and loop over them and then get posts for each category... Since get_posts() doesn't return any information about the category. I guess there is a better way ?

5
  • "For now, I will get my categories and loop over them and then get posts for each category" - how about the other way round - loop through the posts and collect the categories, then filter/group them accordingly. The good side is, there'd be just one WP_Query call, but more processing upon displaying the posts.
    – Sally CJ
    Feb 29, 2020 at 23:22
  • Because in this way, I think I will have more queries, since I have more posts than categories Mar 1, 2020 at 9:11
  • Then I guess you already are using a good approach and can keep doing it that way.. (do correct me if I misunderstood your previous comment..)
    – Sally CJ
    Mar 1, 2020 at 15:10
  • Honestly you are right, it's just weird form me that you can't do a jointure easily on the terms / caterogies from the main query. But my approach is working. I will close my ticket soon since I am looking for tuning. Mar 1, 2020 at 15:49
  • get_posts() or WP_Query only returns post/WP_Post objects (or IDs if fields is set to ids) and not terms. There's the tax_query parameter for querying posts in certain taxonomies, but the returned data would still be post objects/IDs. To get the post categories, you'd need to use functions like get_the_category() or get_the_terms() for custom taxonomies.
    – Sally CJ
    Mar 1, 2020 at 16:00

0

Your Answer

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

Browse other questions tagged or ask your own question.