0

So the site i am working on has a Custom Post type of "Case Results" and one of the columns is "Type of Case". On a certain page I need to get all of these "Case Results with a "Type of Case" of "Criminal Defense Case". I can't seem to figure out which arguments will get me this in WP_Query.

I have tried many combinations but my most recent attempt looks like:

        $args = [
            'post_type' => 'case-results',
            'tax_query' => [
                [
                    'taxonomy' => 'type-of-case',
                    'field' => 'slug',
                    'term' => 'criminal-defense-case'
                ]
            ]
        ];
1

The proper argument to your tax query is terms, not term. You can see this in one of the examples in the Codex:

$args = array(
    'post_type' => 'post',
    'tax_query' => array(
        array(
            'taxonomy' => 'people',
            'field'    => 'slug',
            'terms'    => 'bob',
        ),
    ),
);
$query = new WP_Query( $args );

https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

Assuming that your post types, and taxonomies are what they appear to be, and the slugs are correct, with that change your code should work.

  • I have changed 'term' to 'terms' and still don't seem to be getting the correct results. I am a career developer but am just starting to work with wordpress so it may very well be that I have some other thing off by one letter as well – Bill Garrison May 12 '15 at 22:37
  • Run your query (corrected) and post the SQL. To do that for the example above I'd do var_dump($query->request); – s_ha_dum May 12 '15 at 23:00
  • SELECT SQL_CALC_FOUND_ROWS wp_msh_posts.ID FROM wp_msh_posts WHERE 1=1 AND 0 = 1 AND wp_msh_posts.post_type IN ('post', 'page', 'attachment', 'case-results', 'soliloquy', 'custom_type') AND (wp_msh_posts.post_status = 'publish' OR wp_msh_posts.post_author = 1 AND wp_msh_posts.post_status = 'private') GROUP BY wp_msh_posts.ID ORDER BY wp_msh_posts.post_date DESC LIMIT 0, 6 – Bill Garrison May 14 '15 at 13:20

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.