Title: get_posts issue
Last modified: August 20, 2016

---

# get_posts issue

 *  [Aaron Wagner](https://wordpress.org/support/users/ajwagner777/)
 * (@ajwagner777)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/get_posts-issue-1/)
 * I am having trouble with get_posts and I have been all over the internet for 
   2 days and no matter what, I cannot get this working. Here is the scenario:
 * I have a custom post type, “Events” that has a couple of custom fields, and custom
   taxonomy. That all works perfectly.
 * Where I am running into a wall is in my template when I query that custom post
   using get_posts, it returns either nothing, or an incorrect dataset.
 * In my template, I have a “sidebar” where I want to list upcoming events, with
   a link to that event. I want the user to be able to narrow that list down by 
   a category. When I leave the category out of the $args array, it returns all 
   the posts (expected). However, if I put a category ID it breaks and returns nothing.
   I know there are posts in the particular category, so that’s not the issue.
 * Here is some code that deals with this issue:
 *     ```
       $event_args = array(
       	'numberposts'=>$meta_values['right_events_display'][0],
       	'category'=>$meta_values['right_events_category'][0],
       	'post_type'=>'tf_events'
       );
       $events = get_posts($event_args);
       print_r($events);exit;
       ```
   
 * The above outputs:
 * _[code moderated according to [Forum Rules ](http://codex.wordpress.org/Forum_Welcome)-
   please use the [pastebin](http://pastebin.com/)]_
 * The two meta values in the `$args` array above are values from the custom fields
   on the page where this will be displayed.
 * Anyone got any ideas what I am doing wrong?

Viewing 7 replies - 1 through 7 (of 7 total)

 *  Thread Starter [Aaron Wagner](https://wordpress.org/support/users/ajwagner777/)
 * (@ajwagner777)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/get_posts-issue-1/#post-2119637)
 * In case anyone is interested, here is the code to register the post type:
 * _[code moderated according to [Forum Rules ](http://codex.wordpress.org/Forum_Welcome)-
   please use the [pastebin](http://pastebin.com/)]_
 *  Thread Starter [Aaron Wagner](https://wordpress.org/support/users/ajwagner777/)
 * (@ajwagner777)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/get_posts-issue-1/#post-2119653)
 * Here is the code that registers the post type:
    [http://pastebin.com/BnMA1up6](http://pastebin.com/BnMA1up6)
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/get_posts-issue-1/#post-2119703)
 * try with “cat” (get_posts() can use the WP_Query class parameters):
    `'cat' =
   > $meta_values['right_events_category'][0],` or with an ID: `'cat' => 10,`
 * and check the meta array:
    `print_r($meta_values);`
 * or use [http://codex.wordpress.org/Function_Reference/WP_Query#Taxonomy_Parameters](http://codex.wordpress.org/Function_Reference/WP_Query#Taxonomy_Parameters)
 * if it is a custom taxonomy ID you might have to do something like this for “cat”:
 *     ```
       'tax_query' => array(
       		'relation' => 'OR',
       		array(
       			'taxonomy' => 'tf_eventcategory',
       			'field' => 'id',
       			'terms' => array( $meta_values['right_events_category'][0] ),
       		)
       	)
       ```
   
 *  Thread Starter [Aaron Wagner](https://wordpress.org/support/users/ajwagner777/)
 * (@ajwagner777)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/get_posts-issue-1/#post-2119738)
 * Awesome, I now have it fully functional! Thank you.
 * Followup question: The query returns duplicate events if they are assigned to
   more than one category. Is there a way around this? Not a huge deal because I
   loop through it and assign only the variables I need to an array with the ID 
   as the index. Not a huge deal with 3-4 events, but might become a bigger deal
   later with 300-400 events.
 * FYI, here is the code that I ended up using:
 *     ```
       $event_args = array(
       	'tax_query' => array(
       		'relation' => 'OR',
       		array(
       			'taxonomy' => 'tf_eventcategory',
       			'field' => 'id',
       			'terms' => $meta_values['right_events_category'][0]
       )));
       $query = new WP_Query($event_args);
       ```
   
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/get_posts-issue-1/#post-2119739)
 * Not sure about this one. I will have to test this but right now I don’t have 
   the time. try with `'relation' => 'AND',`
    Don’t think it will work though.
 *  Thread Starter [Aaron Wagner](https://wordpress.org/support/users/ajwagner777/)
 * (@ajwagner777)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/get_posts-issue-1/#post-2119740)
 * You’re correct… that had no effect. No worries, thanks for your assistance!
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/get_posts-issue-1/#post-2119742)
 * If I have time tomorrow I will try and get it done.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘get_posts issue’ is closed to new replies.

## Tags

 * [get_posts](https://wordpress.org/support/topic-tag/get_posts/)
 * [taxonomy](https://wordpress.org/support/topic-tag/taxonomy/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 7 replies
 * 2 participants
 * Last reply from: [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * Last activity: [14 years, 11 months ago](https://wordpress.org/support/topic/get_posts-issue-1/#post-2119742)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
