0
votes

Attempting to loop through a set of categories and display the title of the latest post from each.

  $feed_sources = array('goose-creek','sleepy-creek','fobr');
  foreach ($feed_sources as $feed) {
    $args = array('category_name' => $feed, 'posts_per_page' => 1);
    $show = get_posts($args); 
    print_r($show);

This code returns

Array ( [0] => WP_Post Object ( [ID] => 79 [post_author] => 1 [post_date] => 2015-03-19 08:58:40 [post_date_gmt] => 2015-03-19 09:58:40 [post_content] => 

But I've had no luck in accessing it with $show[0]['post_title'], $show[0][post_title], or $show[0]->'post_title'

Also, is there a simple way to get this array to work with basic theming functions such as the_title(); the_content(); etc?

1
Use $show[0]->post_title - Kostya Shkryob
WP functions usually use global $post variable. You can set it's value to make them work. - Kostya Shkryob
Doesn't return anything :( - Michael Roach
How do you set the global $post variable in this case? - Michael Roach

1 Answers

3
votes

You should re-write it into something like this:

$feed_sources = array('goose-creek','sleepy-creek','fobr');

foreach ($feed_sources as $feed) {
    $args = array('category_name' => $feed, 'posts_per_page' => 1);

    // we are creating new WP_Query object instead using get_posts() function 
    $shows = new WP_Query($args);

    $shows->the_post();
    // now you can use the_title() and the_content()
    the_title();
}

Hope this helps.