Support

Account

Home Forums General Issues Relationship Field order

Solved

Relationship Field order

  • hi,

    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; ?>

    thanks

  • 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.

  • hi John,

    thank you for your response. i found the simplest way to fixed this issue in the Backend with drag & drop

    thanks

  • 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.

  • Hey @hube2

    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_type’=> ‘software’,
    ‘post_in’ => $posts,
    ‘key’=> ‘post_like_count’,
    ‘orderby’ => ‘meta_value_num’,
    ‘order’ => ‘DESC’);
    $the_query = new WP_Query( $args );’

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Relationship Field order’ is closed to new replies.