Support

Account

Home Forums General Issues In loop Sort by ACF field?

Solving

In loop Sort by ACF field?

  • I have a events custom posttype i am using and i need them to sort by the ACF sub_field_ race_date but no matter what i do i cannot get it to work will you take a look and help me please.

    the field is in a repeater section on my field group.

    here is my code:

    <?php 
    					
    						$loop = new WP_Query( array( 
    							//$race_date = get_post_meta( $post->ID, 'add_event_0_event_date', true ),
    							'post_type' => 'event',  
    							'meta_key' => 'add_event_0_event_date',
    							'orderby' => 'meta_value',
    							//'orderby'=>$race_date , 
    							'order' => 'ASC',
    							'posts_per_page' => 150 
    							) );   while ( $loop->have_posts() ) : $loop->the_post();
    						if ( get_post_meta( $post->ID, 'add_event_0_event_date', true ) != '' ) {
    							the_title( '<li class="single_event" style="margin-top: 0px;" ><span class="left-loc event_title event-title  ' . the_title_attribute( 'echo=0' ) . '">
    							          <a onClick="_gaq.push([\'_trackEvent\', \'Slider Menu\', \'click\', "' . the_title_attribute( 'echo=0' ) . '", \'1\', true]);" href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="Event">' );
    							echo "<span class=\"right-loc event_date\">";
    							echo date( 'd.m', strtotime( get_post_meta( $post->ID, 'add_event_0_event_date', true ) ) );
    							echo "</span></a></span></li>";
    							wp_reset_postdata();  }  endwhile;  //end of loop for events listings - pm ?>

    here is my original code

    	<?php $loop = new WP_Query( array( 'post_type' => 'event',  'orderby'=>'race_date', 'posts_per_page' => 10 ) );   while ( $loop->have_posts() ) : $loop->the_post();
    						if ( get_post_meta( $post->ID, 'add_event_0_event_date', true ) != '' ) {
    							the_title( '<li class="single_event" style="margin-top: 0px;" ><span class="left-loc event_title event-title  ' . the_title_attribute( 'echo=0' ) . '">
    							          <a onClick="_gaq.push([\'_trackEvent\', \'Slider Menu\', \'click\', "' . the_title_attribute( 'echo=0' ) . '", \'1\', true]);" href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="Event">' );
    							echo "<span class=\"right-loc event_date\">";
    							echo date( 'd.m', strtotime( get_post_meta( $post->ID, 'add_event_0_event_date', true ) ) );
    							echo "</span></a></span></li>";
    							wp_reset_postdata();  }  endwhile;  //end of loop for events listings - pm ?>

    the original code works fine but i cannot get it to sort by the acf field, i can only get it to display it.

  • Try using meta_query and specifying ‘type’ => ‘DATE’. Or you could try using “meta_value_num” as the ‘orderby’ argument, but I can’t remember if this works or not. See: http://codex.wordpress.org/wp_query#Custom_Field_Parameters

  • if i use DATE it uses the post creation date of the event not the event start date. i need to be able to add the acf race_date custom field to the loop somehow but i cannot get it.

  • Hi @madsynn

    Can you please clarify what you are trying to do?

    Your code suggests that you want to sort by a sub field? Is this correct?
    Perhaps you could state what fields you have and how this sub field is related to the posts?

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

The topic ‘In loop Sort by ACF field?’ is closed to new replies.