WordPress.org

Ready to get started?Download WordPress

Forums

Very Specific query needed (2 posts)

  1. stayfq
    Member
    Posted 2 years ago #

    I need to display the 5 latest entries of a custom post type. Not a problem:

    $args = array(
    							'post_type' => 'show',
    							'posts_per_page' => 5,
    							'orderby' => 'meta_value',
    							'meta_key' => 'show_date',
    							'order' => 'DESC'
    						);
    						$home_shows = new WP_Query($args);

    I'm stumbling with the next part though: I need to then display those latest posts starting with the earliest post.

    In essence, I need to take the last 5 posts, then reverse their order.

    I tried using the php function 'array_reverse,' but ended up with some really funky stuff happening (it successfully reversed the array, but I was getting very odd content showing up).

    Any ideas?

  2. zimmi88
    Member
    Posted 2 years ago #

    Hmm... what do you mean by "odd content"? What I can think of off the top of my head is that there might be some usage of template tags or some inner loop that's messing with the $post variable.

    The constructor will return an instance of WP_Query, so you'd want to run array_reverse on $home_shows->posts, not $home_shows itself. You can try checking the result of this by running print_r on the variable after reversing it (you can echo its output into some sort of temporary absolute-placed div just to view the results).

    Other than that, the logic seems pretty sound by running the query then reversing the results. From there, you can use the examples from the WP_Query page to run an inner loop.

    If you're still having troubles finding the issue, if you could post back your output code, it might help with figuring out the issue.

    I hope this helps!

Topic Closed

This topic has been closed to new replies.

About this Topic