• Hello!

    I am using ACF PRO’s Repeater feature which allows for one or multiple subfield within a single post. I am utilizing this feature so that content editors can assign multiple dates to a post with an assigned priority level (high=1 and low=0) for each date.

    The code below queries for posts that have an assigned date matching todays date. Then I want to order the posts so that the assigned date priority level 1 posts are listed first. Everything works as expected but when I have posts with more than one assigned date/priority level, the query just utilizes the first priority value it gets, not just the priority value which is assigned to the specific (today’s) date.

    I somehow need to identify the row # the assigned date is from so that I can only utilize the priority value from that sub-row. Any help would be amazing! Thank you in advance!


    If you’re not familiar with ACF Repeater feature, they use a three-part naming convention for repeating sub-fields:
    1. main-custom-field-name
    2. row#
    3. sub-field-name
    OR
    main-custom-field-name_row#_sub-field-name

    <?php
    
    function my_posts_where( $where ) {
    	$where = str_replace("meta_key = 'ssm_assign_%", "meta_key LIKE 'ssm_assign_%", $where);
    	return $where;
    }
    add_filter('posts_where', 'my_posts_where');
    
    $datetoday = current_time('Ymd');
    $args = array(
    	'posts_per_page'=> 2,
    	'post_type'		=> 'ssm',
    	'meta_query' 	=> array(
            'relation' 			=> 'AND',
    		'assigned_dates' 	=> array(
    			'key'				=> 'ssm_assign_%_ssm_assigned_date',
    			'compare'			=> '=',
    			'value'				=> $datetoday,
    		),
            'assigned_priorities' => array(
                'key'     			=> 'ssm_assign_%_ssm_date_priority',
    			'compare' 			=> 'EXISTS',
            ),
    	),
        'orderby' 		=> 'assigned_priorities',
        'order' 			=> 'DESC',
    
    );
    // query
    $the_query = new WP_Query( $args );
    if( $the_query->have_posts() ):
    
    	// Start the loop.
    	while ( $the_query->have_posts() ) : $the_query->the_post();
    		include(locate_template('content.php'));
    	endwhile;
    	// End the loop.
    		get_template_part( 'content', 'none' );
    	endif; //End of customized current day posts
    
    wp_reset_query();	 // Restore global post data stomped by the_post().
    
    ?>
  • The topic ‘Sub-field query post order’ is closed to new replies.