0

I'm using get_posts to grab list of posts from custom post_type. I also want list to be limited to specific category. So I tried:

$postslist = get_posts('order=ASC&orderby=date&posts_per_page=4&post_type=tribe_events&category_name=Gudauri');

This displays nothing, if I remove category_name=Gudauri it lists all posts from custom post type tribe_events.

What am I doing wrong? I also tried category=id but with no luck!

1 Answer 1

1

Edit

In response to OP's answer:

So you use post_type and add cat at the end. Like [tribe_events][_cat]=[value]

This is absolutely incorrect. WordPress does not automatically create custom taxonomies in this manner.

From the OP's comment:

I'm using tribe Events calendar. Check it there.

Here is the relevant PHP file from the The Events Calendar Plugin. And here are the key lines of code:

const TAXONOMY = 'tribe_events_cat';
const POSTTYPE = 'tribe_events';

...and...

register_taxonomy( self::TAXONOMY, self::POSTTYPE, array( /**snipped*/));

As suspected: the Plugin is registering a custom post type, and then registering a custom taxonomy to use for that custom post type.

Original Answer

So, you've registered a custom post type, 'tribe_events'. Two important points:

  1. By default, custom post types have no registered/supported taxonomies, including the category taxonomy.
  2. By default, the 'category' taxonomy is only registered/supported by the 'post' post-type.

In order to use the 'category' taxonomy for a custom post type, you need to do one of two things:

  1. Add 'category' to the array of taxonomies supported by the custom post type, via the 'taxonomies' parameter of the register_post_type() arguments array:

    register_post_type(
        'tribe_events',
        array(
            'taxonomies' => array( 'category' )
        )
    );
    
  2. Add 'category' taxonomy support for the custom post type, via register_taxonomy_for_object_type():

    register_taxonomy_for_object_type( 'category', 'tribe_events' );
    

Regarding your proposed "solution"

From your answer:

So you use post_type and add cat at the end. Like [tribe_events][_cat]=[value]

I know of absolutely nothing in core that would auto-create a {post-type}_cat taxonomy, or that would equate a {post-type}_cat taxonomy to the category taxonomy.

What I suspect is happening here is that you've registered a custom 'tribe_events_cat' taxonomy, and then added that custom taxonomy to your custom post type, via the 'taxonomies' parameter in register_post_type().

If this is the case, then:

  1. Your question is misleading, because you indicate that you want to use the 'category' taxonomy in your query
  2. I would strongly recommend getting in the habit of referring to a custom taxonomy as a taxonomy, rather than as a category. "Category" is a taxonomy; not all taxonomies are "category".
1
  • I didn't do anything, I'm using tribe events calendar. So I was guessing how authors of the plugin implemented it. I accidentally discovered how to display posts by going through stackoverflow topics.
    – user8842
    Aug 30, 2012 at 21:01

Your Answer

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