0

So I've created a custom taxonomy type with a plugin (CMS Press) and now I'm trying to get all of the posts with the taxonomy terms in that taxonomy.

My get_posts looks like this:

$args = Array( 
    'numberposts' => 5, 
    'offset' => 0,
    'post_status' => 'publish',
    'content-type' => 'my-term'
);

get_posts($args);

I've also tried:

$args = Array( 
    'numberposts' => 5, 
    'offset' => 0,
    'post_status' => 'publish',
    'tax_query' => Array( Array ( 
        'taxonomy' => 'content-type' 
        'field' => 'slug' 
        'terms' => 'my-term' 
    ));

get_posts($args);

This works fine when I'm not using a custom taxonomy. if I change my request to:

$args = Array( 
    'numberposts' => 5, 
    'offset' => 0,
    'post_status' => 'publish',
    'tax_query' => Array( Array ( 
        'taxonomy' => 'post_tags' 
        'field' => 'slug' 
        'terms' => 'some-tag' 
    ));

get_posts($args);

it works.

I was wondering if there's something I'm leaving out that's preventing custom taxonomies from being queried. Any help would be appreciated.

4
  • Hey Matt, I'm not familiar with the version of filling an array you show here. All the methods I've seen use syntax like $args = array( 'numberposts' = -1, 'post_status' = 'publish' ); etc. In case that helps. You can fill an array $arg['numberposts'] = -1 but again that's different to what you show. See php.net for more.
    – Jon
    Commented Aug 2, 2013 at 0:16
  • first, I assume you're actually calling get_posts (plural) and get_post is just a typo, also post_tags (the built in taxonomy is singular, post_tag). for the purpose of debugging I suggest using WP_Query rather than get_posts, then you can var_dump the query object to see the actual query being generated and sent to the database.
    – Milo
    Commented Aug 2, 2013 at 0:44
  • Sorry, I was formatting based on some debug output. Assured that in the actual code I'm using the array syntax (and get_posts) correctly. I'll edit to reflect that. Commented Aug 2, 2013 at 0:50
  • Normally should work... Just in case, the custom taxonomy is applied to normal posts, right? Commented Jun 25, 2014 at 21:43

2 Answers 2

5

From the WordPress codex for wp_query:

Display posts tagged with bob, under 'people' custom taxonomy:

$args = array(
    'post_type' => 'post',
    'people' => 'bob'
);
$query = new WP_Query( $args );

Display posts tagged with bob, under 'people' custom taxonomy, using tax_query:

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

See the Codex page for more examples.

2
  • This is what we've tried. it hasn't worked (please see above). Commented Aug 2, 2013 at 1:37
  • For me this works. Commented Nov 22, 2016 at 17:55
1

The most probable cause of this issue is in the use of hypens (-) in your custom taxonomy name. Hyphens are extremely troublesome when used in custom taxonomy names, so by rule they should be avoided at all costs.

If you need to separate names in a custom taxonomy name, or even a custom post type name, you should and only use underscores (_). Also avoid the use of camelcase, numbers as first character and any type of special charachers

Your Answer

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

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