1

What I am trying to do

Hi, in my site I have a custom post type called projects, then I have created a custom taxonomy for my projects post type called project_catagories. Now in my projects archive page (where it shows all the projects), I have a section called browse by category and when a user clicks on a category name it is supposed to show the projects under that category.

Now to show the projects under the clicked category, I've created a template file called taxonomy-project_catagories.php which shows each project category projects. Now within that file, I am first using the get_term_by() to get the term_id of that page like the following way:

$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );

Then I'm passing that term_id to my WP_Query() to get the list of projects under that category, like this:

$args = array(
  'numberposts'  => -1,
  'post_type'    => 'projects',
  'tax_query'    => array (
    'taxonomy' => 'project_catagories',
    'field'    => 'term_id',
    'terms'    => $term->term_id
  )
);

$the_project_cat_query = new WP_Query($args);

Then finally I loop through and show the projects.

Problem:

Now the problem is that no matter what term_id is being passed to the WP_Query(), it is showing up all the projects under all categories. I know there are many similar questions here on this forum and stack overflow and I have checked them all but none helped me. I even checked WP_Quey documentation and still couldn't figure out what's happening. So, any help will be really helpful.

How I tested it

I have created one project under one project category, so technically when will be on that project category page, then only it should show me the project details and for other pages, it should show blank as there are no categories under that project category.

II even tried manually entering the term_id, checking the field value to slug and then manually entering a slug. In all cases exactly the same result.

Any response will be really helpful.

1

Taxonomy Parameters

Important Note: tax_query takes an array of tax query arguments arrays (it takes an array of arrays).

This construct allows you to query multiple taxonomies by using the relation parameter in the first (outer) array to describe the boolean relationship between the taxonomy arrays.

In 'tax_query' you should add array of arrays (see in Codex):

'tax_query'    => array (
    array(
        'taxonomy' => 'project_catagories',
        'field'    => 'term_id',
        'terms'    => $term->term_id
    )
)
| improve this answer | |
  • Oh! My god. How I missed that simple thing? OMG! I'm getting blind day by day. Thank you very much for the helpmate. I have been scratching my head for the last two hours with this. I'm such a blind person. Checked the docs multiple times and forgot to see the array inside array` thing. Shame on me. Thanks again for the fast reply. :) – iSaumya Aug 6 '19 at 7:45

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.