Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using the postMash plugin to allow a client to be able to easily reorder posts without having to change the timestamp on each one, and it works fine. However, I'm using a custom loop to call the latest posts in a category onto the home page like so:

$i=0;
$events = get_posts( array ( 'category' => 6, 'orderby' => 'menu_order', 'order' => 'ASC' ) );
if ( $events ) {
    echo '<ul>';
    foreach ( $events as $event )  {
        $eventDate = get_post_meta($event->ID, 'event-date', true );
        $eventTime = get_post_meta($event->ID, 'event-time', true);
        echo "<li>".$eventDate;
        if ($eventDate && $eventTime) {
            echo " - ";
        }
        echo $eventTime."<br>";
        echo "<a href='".get_permalink($event->ID)."'>".get_the_title($event->ID)."</a></li><br>";
    if (++$i == 3) break;
}
echo '</ul>';

}

The plugin successfully reorders the posts on the category page, but not within this custom loop..

Here's the plugin url: http://wordpress.org/extend/plugins/postmash/

share|improve this question
add comment

1 Answer 1

up vote 0 down vote accepted

That plugin is 3 years old. I'm not sure if it uses the menu_order column to set the order but if it does you can filter pre_get_posts or your custom query with:

'orderby' => 'menu_order',
'order'   => 'ASC'

This would all have to be done before the $post object is added to the $events array so it would help answer if you added the code that comes before: if ( $events )

Also if you do a var_dump( $event ) somewhere in your foreach loop we can figure out how the posts are being ordered.

Edit

After your comments and looking at the var_dump here is whats going on:

Your getting 5 posts in proper menu order then your break statement causes the foreach loop to start back at 0 then it posts the same 5 posts again. It does this 3 times giving you

1-2-3-4-5 break 1-2-3-4-5 break 1-2-3-4-5 break

See the formated $post array dump: http://pastebin.com/hHPxbzda

If you only want to show the first 3 just add this to your get_posts array:
'posts_per_page' => 3

share|improve this answer
    
I added the code before - already called orderby and order.. what values from var_dump would you like me to paste here? –  Northernlights May 3 '12 at 9:18
    
and do you know if there are any newer plugins that might achieve the same effect? –  Northernlights May 3 '12 at 9:19
    
var_dump will give you the menu_order assigned to each each post. All new posts have a default menu_order of 0 which would make them come first unless you create a save_post hook that changes it to something else. –  Chris_O May 3 '12 at 9:53
    
I don't think there is a plugin that does this well but in this question all the code is laid out to make it work. –  Chris_O May 3 '12 at 9:57
    
it's dumping 3 menu_order values per post.. take a look at antonleroux.co.za –  Northernlights May 3 '12 at 10:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.