Support

Account

Home Forums General Issues Order ACF relationship by acf date field of posts?

Solving

Order ACF relationship by acf date field of posts?

  • Hello,

    I am having a hell of a time figuring out how to adapt the basic acf relationship loop with setup postdata to order by the ACF date field of the posts.

    Specifically it would be nice to see an actual example of how to add the query parameters to the example in the documentation

  • 
    // get relationship field without formatting
    // returns an array of post ID values
    $posts_ids = get_field('relationship_field', false, false);
    // do your own query to order the posts
    $args = array(
      'post_type' => 'any',
      'posts_per_page' => -1,
      'post__in' => $post_ids,
      'meta_query' => array(
        'date_clause' => array(
          'key' => 'date_field',
          'value' => 'EXISTS'
        ),
      ),
      'orderby' => array('date_clause' => 'ASC');
    );
    $custom_query = new WP_Query($args);
    if ($custom_query=>have_posts()) {
      while ($custom_query=>have_posts()) {
        $custom_query=>the_post();
        // code continues...
      }
    }
    wp_reset_postdata();
    
  • Hey @hube2

    Thanks for your help here. I am just touching back on this now. I put my repeater and date fields in the two specified areas, along with the_title in the loop area so my code looks like this:

    <?php // get relationship field without formatting
    // returns an array of post ID values
    $posts_ids = get_field('seminars', false, false);
    // do your own query to order the posts
    $args = array(
      'post_type' => 'any',
      'posts_per_page' => -1,
      'post__in' => $post_ids,
      'meta_query' => array(
        'date_clause' => array(
          'key' => 'event_time',
          'value' => 'EXISTS'
        ),
      ),
      'orderby' => array('date_clause' => 'ASC');
    );
    $custom_query = new WP_Query($args);
    if ($custom_query=>have_posts()) {
      while ($custom_query=>have_posts()) {
        $custom_query=>the_post();
        the_title();
      }
    }
    wp_reset_postdata();
    ?>

    However I get a php error at this line:
    'orderby' => array('date_clause' => 'ASC');

    [08-Oct-2018 14:29:58 UTC] PHP Parse error: syntax error, unexpected ‘;’, expecting ‘)’ in /home/clientname/public_html/wp-content/themes/mytheme/template-parts/content-attorney.php on line 182

    I’ve been playing find the syntax error with no luck. Anything glaringly obvious to you?

  • Just remove the offending semi-colon, that’s a typo.

  • I should mention what I am really trying to do here is have a way to display upcoming and past events separately for events that are assigned to a specific post. This seems to be working for me:

    <?php
    	$today = date( 'Ymd' );
    	$post_ids = get_field('seminars', false, false);
    
    	$posts = get_posts(array(
    		'post_type' => 'any',
    		'posts_per_page'	=> -1,
    		'post__in'	=> $post_ids,
    		'meta_key'       => 'event_date',
    		'meta_compare'   => '>=',
    		'meta_value'     => $today,
    	));
    
    	if( $posts ) {
    		?>
    		<ul>
    			<?php foreach( $posts as $post): ?>
    				<?php setup_postdata($post); ?>
    				<li class="line">
    					<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    					<?php the_excerpt(); ?>
    				</li>
    			<?php endforeach; ?>
    		</ul>
    		<?php wp_reset_postdata(); ?>
    		<?php } ?>
Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Order ACF relationship by acf date field of posts?’ is closed to new replies.