Home › Forums › General Issues › Relationship Field order
i have an relationship field that display the post. How can i order the posts by DESC? default is ASC
<?php $posts = get_field('post_category_relationship'); if( $posts ): ?> <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?> <div class="mt-6"> <div class="flex flex-wrap border border-grey-lighter mb-6 rounded shadow-lg relative"> <div class="w-1/3 sm:w-full sm:pr-0"> <?php setup_postdata($post); ?> <a class="-mb-1 block" href="<?php the_permalink(); ?>"> <?php $post_img = get_imgproxy_url(get_the_post_thumbnail_url() , 400, 269, 'fill' , 'sm'); ?> <img src="<?php echo $post_img; ?>" alt="" class="w-full lg:rounded-l md:rounded-tl sm:rounded-t"> </a> </div> <div class="w-2/3 sm:w-full p-4"> <a class="no-underline hover:underline text-secondary" href="<?php the_permalink(); ?>" title="weiterlesen"> <h2 class="mb-4"><?php the_title(); ?></h2> </a> <?php $excerpt = get_field('excerpt'); echo '<div class="mb-4"><p>'; echo $excerpt; echo ' [...]</p></div>' ?> <div class="absolute read-more-button"> <a class="bg-secondary text-white no-underline px-4 py-2 hover:bg-secondary-light font-bold" href="<?php the_permalink(); ?>" title="Read more">zum Beitrag</a> </div> <p><i class="fal fa-calendar mr-2" aria-hidden="true"></i><?php echo get_the_date(); ?> | <?php if( have_rows('choose_agent_section')): while ( have_rows('choose_agent_section')) : the_row(); // set up post object $post_object = get_sub_field('choose_agent'); if( $post_object ) : $post = $post_object; setup_postdata($post); ?> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p> <?php endif; endwhile; ?> <?php endif; ?> <div class="tags mb-4"> <p><i class="fal fa-tags"></i><?php the_tags(' '); ?></p> </div> </div> </div> </div> <?php endforeach; ?> <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?> <?php endif; ?>
A relationship field will return the posts in the order that they appear in the relationship field, basically the order that they are added.
There is only one way that I know of to alter this order.
1) Set up the field to return the post IDs instead of post objects. Alternately you can get just the post IDs by using the 3rd argument for get_field() to get the unformatted value
get_field('post_category_relationship', false, false);
2) Query the posts with your own WP_Query() using the value returned for the field in the
post__in argument and construct the rest of the arguments to sort them in the order that you want.
thank you for your response. i found the simplest way to fixed this issue in the Backend with drag & drop
I suggested the second option because I didn’t know how many posts you’d be contending with. If you have a long list, dragging them to the right order could become tedious.
Just wondering how that would work.
I have a relationship field called “just like”. I use the following code to spit out post ids.
$posts = get_field('just_like', false, false);
This works well and returns and array of all post ids.
Now each post has a meta field called vote.
How would I filter all of them via vote.
I tried the following
`$args = array(
‘numberposts’ => -1,
‘post_in’ => $posts,
‘orderby’ => ‘meta_value_num’,
‘order’ => ‘DESC’);
$the_query = new WP_Query( $args );’
post_in needs to be
You must be logged in to reply to this topic.
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!
🤔 Curious about the ACF user experience? So are we! Help guide the evolution of ACF by taking part in our first ever Annual Survey and guarantee you’re represented in the results. https://t.co/0cgr9ZFOJ5— Advanced Custom Fields (@wp_acf) May 8, 2023
© 2023 Advanced Custom Fields.