Home › Forums › Front-end Issues › order by sorting in relationship field
Hello,
Basically, I have this exact question: http://wordpress.org/support/topic/advanced-custom-fields-relationship-field-sort-order
But this topic was closed and points to a 404 page so no help there or anywhere else as far as I could find.
I am trying to get the posts queried with the relationship field to show in the order they are dragged by the user.
The fact that the selected posts in the relationship field are dragable/sortable suggests this is possible, but I can’t figure it out.
This is my code:
$ids= get_field('relationshipfieldname');
if (!empty($ids)) {
$relations= new WP_Query(array(
'posts_per_page' => 4,
'post__in' => $ids,
'post_status' => 'publish',
'post_type' => 'page'
));
if ( $relations->have_posts() ) {
while ( $uitgelicht->have_posts() ) {
$uitgelicht->the_post();
//the format in which the posts are shown
}
}
wp_reset_postdata();
}
I’ve tried menu_order, and other stuff but nothing seems to change the order.
Try this:
<?php
// Query Arguments
$ids = get_field('relationship_name', false, false);
$args = array(
'post__in' => $ids,
'orderby' => 'post__in'
);
// The Query
$the_query = new WP_Query( $args);
// Check if the Query returns any posts
if ( $the_query->have_posts() ) {
php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h1><?php the_field('postField_name'); ?></h1>
<?php endwhile; } ?>
Ups…
Change:
php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
to:
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
Hey @franciscofuentes – A big THANK YOU for posting this. I had no idea you could ‘orderby” => ‘post__in’. Solved an issue for me today! Cheers! 🙂
Hi. I hope to make exactly same orderby rule.
I added this code on my website at on function.php
file but it isn’t work.
Please let me know if I have to edit different file or change this code.
The topic ‘order by sorting in relationship field’ 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.