Support

Account

Home Forums Add-ons Repeater Field Query Last Row of a Repeater Field

Solving

Query Last Row of a Repeater Field

  • Hi,

    I’m trying to query the last row of a repeater field so that I can display posts by date based on a specific field in the last row.

    I have a field called ‘record’ with two subfields ‘date_in’ and ‘date_out’.

    So far I can query the ‘date_in’ sub field and it orders my posts, but only based on the first row of the repeater field rather than the last.

    Any help appreciated. This is what I have so far:

    // filter
    function my_posts_where( $where ) {	
    	$where = str_replace("meta_key = 'record_%", "meta_key LIKE 'record_%", $where);
    	return $where;
    }
    
    add_filter('posts_where', 'my_posts_where');
    
    // args
    $args = array(
    	'numberposts'	=> -1,
    	'post_type'	=> 'post',
    	'meta_query'	=> 'record_%_date_in',
    	'order'		=> 'ASC',
    	'orderby'	=> 'meta_value',
    	'meta_type'	=> 'DATETIME'	
    );
    
    // query
    $the_query = new WP_Query( $args);
  • There isn’t any way to do a query based on the last row of a repeater field. In order to do that you would need to have the same number of rows in every repeater and you’d need to replace the % in the query with the row number. For example, if a post has 5 rows the you’d query for ‘record_4_date_in’.

    Querying by and ordering by repeater sub fields is always problematic and inconsistent. Rather than try to do this I’ve found that creating a consistent field that can be used is a better solutions. For example, lets say that you want to sort your posts by the last date_in sub field.

    
    add_filter('acf/update_value/name=record', 'my_create_a_usable_field', 10, 3);
    function my_create_a_usable_field($value, $post_id, $field) {
      // $value will hold a nested array with the rows of the repeater
      if (!is_array($value)) {
        // the repeater is empty, bail early
        return $value;
      }
      $last_row = end($value);
      $date_in = $last_row['date_in'];
      // now put it into a different field
      // this field can be used in queries for filtering and sorting
      update_post_meta($post_id, 'filterable_date_in', $date_in);
    }
    

    Please not that you may need to do more with the value, for example, converting it into correct MySQL date format. I’m not sure what the value of a date field will be at this point. At any rate, this is a little more work, but gives you what is needed in a form that’s usable.

  • This reply has been marked as private.
  • Hi, so I realised from the comments I still need to query this new field. I’ve added in the query to order posts by date with the newly created field, unfortunately to no avail. Any Ideas:

    add_filter('acf/update_value/name=record', 'my_create_a_usable_field', 10, 3);
    function my_create_a_usable_field($value, $post_id, $field) {
      // $value will hold a nested array with the rows of the repeater
      if (!is_array($value)) {
        // the repeater is empty, bail early
        return $value;
      }
      $last_row = end($value);
      $date_in = $last_row['date_in'];
      // now put it into a different field
      // this field can be used in queries for filtering and sorting
      update_post_meta($post_id, 'filterable_date_in', $date_in);
    }
    
    // args
    $args = array(
    	'numberposts'	=> -1,
    	'post_type'		=> 'post',
    	'meta_query'		=> 'filterable_date_in',
    	'order'			=> 'ASC',
    	'orderby'		=> 'meta_value',
    	'meta_type'		=> 'DATETIME'	
    );
    
    // query
    $the_query = new WP_Query( $args);
  • The value stored by ACF is not a valid “DATETIME”. try numberic, or just delete the meta_type argument. and ‘meta_query’ for how you’re using it should be ‘meta_key’

    You’ll also need to manually update every post to get the values of the new field into the database.

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

The topic ‘Query Last Row of a Repeater Field’ is closed to new replies.