0

The theme I'm working with has a custom post type, "portfolio", inside a custom taxonomy, "portfoliocat" (post type and taxonomy definitions here). It is working ok: in the backup menu I can see the "Portfolio" option, from where I can create posts of this custom type, or categories to group those posts.

I tried to make a custom widget that lists all the categories in the "portfoliocat" taxonomy, and all the posts under each category. Something like:

= Portfoliocat
=== Category 1
===== Post 1_1
===== Post 1_2
=== Category 2
===== Post 2_1
===== Post 2_2

Instead, this is what I get:

= Portfoliocat
=== Category 1
===== Post 1_1
===== Post 1_2
===== Post 2_1
===== Post 2_2
=== Category 2
===== Post 1_1
===== Post 1_2
===== Post 2_1
===== Post 2_2

That is, for each category I get all the posts that belong to the taxonomy, no matter what category they belong to (instead of those ones that, belonging to the taxonomy, also belong to the category). Code here.

I think the problem is around line 36, when defining "$categoryArgs". No matter what value I assign to the 'category' key, or if I remove it totally, I still get all the posts in the taxonomy.

I have also tried using 'term' instead of 'category' with the same result.

I'd really appreciate some help on this issue.

  • None of the code you've provided is actually to do with outputting the widget. Post your code so that we can help you adjust it. – TheDeadMedic Jul 1 '16 at 9:56
  • @TheDeadMedic There is a link to the code, although I admit it's very easy to miss. – NateWr Jul 1 '16 at 10:03
  • That code is just your register post type. Not really that helpful here. – TheDeadMedic Jul 1 '16 at 10:04
  • No it's farther down. (ps - I'm not the poster) I think I've got an answer for them anyway, so no worries. – NateWr Jul 1 '16 at 10:07
  • Sorry, I thought that being that big amount of code, linking it instead of pasting it in the middle of the question would make the question easier to read. Obviously, I was wrong. – akhasis Jul 1 '16 at 20:05
1

Since you're using a custom taxonomy, you can't use the category argument. You can find some information about custom taxonomy retrieval in this comment in the developer documentation.

According to that, I think your $categoryArgs should be:

$categoryArgs = array(
    'orderby' => 'post_date',
    'order' => 'DESC',
    'post_type' => $type,
    'tax_query' => array(
        array(
            'taxonomy' => $tax,
            'field' => 'slug',
            'terms' => $myPostTax->slug,
        )
    ),
);
  • Yey, that seems to have done the trick! Now I know how to do it it looks so obvious, but I spent days trying variations and looking at the examples at Wordpress codex and forums. Thank you! – akhasis Jul 1 '16 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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