admin管理员组

文章数量:1417406

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.

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.

Share Improve this question edited Aug 6, 2019 at 7:37 iSaumya asked Aug 6, 2019 at 7:22 iSaumyaiSaumya 93619 silver badges36 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

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
    )
)

本文标签: wp querytaxquery returning all posts instead of selective posts in WPQuery