Home › Forums › General Issues › Order Post Objects by menu_order
My custom field is Field Name: child_pages, Field Type: Post Object, Post Type: page, Select multiple values?: Yes. I want to get those child pages and list them in menu order. The code below does not work. It gets the five most recent blog posts ordered by date. It totally ignores the get_field and array parts. What am I doing wrong?
<?php
$ids = get_field('child_pages', false, false);
$post_objects = get_posts(array(
'post__in' => $ids,
'order' => 'ASC',
'orderby' => 'menu_order'
));
$post_objects = get_posts( $args );
if( $post_objects ): ?>
<ul>
<?php foreach($post_objects as $post) : setup_postdata($post); ?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Here are some references:
http://stackoverflow.com/questions/16431853/post-objects-in-advanced-custom-fields-order-by-date
http://old.support.advancedcustomfields.com/discussion/5846/adding-args-to-post_objects-get_field/p1
http://old.support.advancedcustomfields.com/discussion/5024/querying-order-by-title-post_objects/p1
Thank you!
Hi!
My best guess would be that this:
get_field('child_pages', false, false);
does not work anymore.. since you’re getting the 5 latest blogposts it would suggest that $ids is empty and you’re making a standard get_posts call..
What you’re doing is a bit strange tho.. you’ve got a ACF field which fetches all of the posts you want and you want to extract the ids to make a new query which does the very same thing. The only difference is that you want it ordered by menu_order..
If you’re not afraid to do a little bit of digging a better solution would be to copy the post_object field file from ACF core and make your own field which only outputs ID.. I think I’ve made such changes before but I can’t find them now..
Yes, you’re right. The only difference is I want it sorted by menu_order. Do you know how I can do that?
I can get the posts I want using the code below, but then how do I sort them by menu_order?
<?php $post_objects = get_field('child_pages'); // from http://www.advancedcustomfields.com/resources/field-types/post-object/
if( $post_objects ): ?>
<ul>
<?php foreach( $post_objects as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
Thanks!
Well I’m pretty sure menu_order isn’t included in the post object so there’s no way to do this after fetching the array of objects.. Your best bet is to either make a copy of the field like I said or ask elliot to include the option in the next version..
Another solution might be to switch from the post object field to the relationship field.. that way you can control the order of the posts by the order you select them in the field (instead of menu order)
Install the plugin Post Types Order
Solution is use:
function objectRSort(&$object, $key)
{
for ($i = count($object) - 1; $i >= 0; $i--)
{
$swapped = false;
for ($j = 0; $j < $i; $j++)
{
if(is_numeric($object[$j]->$key))
{
if ($object[$j]->$key > $object[$j + 1]->$key)
{
$tmp = $object[$j];
$object[$j] = $object[$j + 1];
$object[$j + 1] = $tmp;
$swapped = true;
}
}
else
{
if (strtolower($object[$j]->$key) > strtolower($object[$j + 1]->$key))
{
$tmp = $object[$j];
$object[$j] = $object[$j + 1];
$object[$j + 1] = $tmp;
$swapped = true;
}
}
}
if (!$swapped) return;
}
}
And just include this code:
$child_pages = get_field('child_pages');
objectRSort($child_pages, 'menu_order');
I did not use the Post Types Order, but put the function from kakaroto84 in my functions.php.
Now my code looks like this:
<?php
$child_pages = get_field('child_pages');
objectRSort($child_pages, 'menu_order');
if( $child_pages ): ?>
<ul class="subpages">
<?php foreach( $child_pages as $post): ?>
<?php setup_postdata($post); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Thank you kakaroto84!
The topic ‘Order Post Objects by menu_order’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.