• 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 - please use the pastebin]

    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

    (@ajwagner777)

    In case anyone is interested, here is the code to register the post type:

    [code moderated according to Forum Rules - please use the pastebin]

    Thread Starter Aaron Wagner

    (@ajwagner777)

    Here is the code that registers the post type:
    http://pastebin.com/BnMA1up6

    Moderator keesiemeijer

    (@keesiemeijer)

    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

    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

    (@ajwagner777)

    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

    (@keesiemeijer)

    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

    (@ajwagner777)

    You’re correct… that had no effect. No worries, thanks for your assistance!

    Moderator keesiemeijer

    (@keesiemeijer)

    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.