Support

Account

Home Forums General Issues Post Object Relational order per ACF Date

Helping

Post Object Relational order per ACF Date

  • So I have two post types. Which are in relation via post object. In a single CPT, I am showing relational posts from CPT 2. Works fine. But I want to order those posts per the ACF date field.

    If I do, the normal loop it shows fine with the order, but if I put a relation thing, which is needed to pull posts via object it’s not working. Can’t figure how to do this. Any ideas?

    <?php

    // query events
    $events = new WP_Query(array(
    ‘post_type’ => ‘company’,
    ‘post_status’ => ‘publish’,
    ‘posts_per_page’ => -1,
    ‘meta_key’ => ‘webinar_date’,
    ‘orderby’ => ‘meta_value’,
    ‘order’ => ‘DESC’,
    )
    );

    ?>

    <?php if ( $events->have_posts() ) : ?>

    <?php while ( $events->have_posts() ) : $events->the_post(); ?>

    <?php
    $featured_posts = get_field(‘relation’);

    if( $featured_posts ): ?>

    <?php foreach( $featured_posts as $post ):

    // Setup this post for WP functions (variable must be named $post).
    setup_postdata($post); ?>

    <?php the_title(); ?>

    <?php endforeach; ?>

    <?php
    // Reset the global post object so that the rest of the page works correctly.
    wp_reset_postdata(); ?>

    <?php endif; ?>

    <?php endwhile; // end of the loop. ?>

    <?php endif;?>

  • You need to get the posts from the post object field without formatting them. This is done by using get_field('post_object', false, false) see get_field() doc.

    The you have to do your own wp_query to get the posts by setting “post__in” to the array returned and order them by the meta_value.

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

You must be logged in to reply to this topic.