Support

Account

Home Forums Add-ons Repeater Field Ordering posts by repeater subfield value, row chosen by associated subfield

Solved

Ordering posts by repeater subfield value, row chosen by associated subfield

  • Hi all,

    I’ve been scratching my head over this for much too long now so I’ve decided to turn to the community for help. I’ve looked over multiple support posts here and else where for an answer but have yet to find one.

    I have a Custom Post Type (CPT) that I need to sort when displaying. The sorting needs to be done by “category” and then further sorted by “order in that category”. It is set up that I will be running one WP_Query per “category”.

    Each instance of the CPT has a repeater field named tmi-categories. This repeater field has two fields in itself: a “category” named tmi-categories-category-name (which is an ACF Select, non-multi) and an “order” named tmi-categories-order (which is just an ACF Number).

    I am currently successfully pulling from the correct category needed but I am failing at using tmi-categories-order to order them. The posts are being ordered by the first tmi-categories-order value in the repeater field.

    For example, if this is the repeater for a particular instance of the CPT:

    • My Cool Category | 5
    • My Other Category | 0

    Then it will always use “5” to order by. This makes sense with how it’s setup but I don’t know how to say “Given this category ($category), use the tmi-categories-order in the same repeater field row where the category matches tmi-categories-category-name.

    Here is the relevant code snippet:

    
    function my_posts_where( $where ) {
    	$where = str_replace("meta_key = 'tmi-categories_$", "meta_key LIKE 'tmi-categories_%", $where);
    	return $where;
    }
    add_filter('posts_where', 'my_posts_where');
    
    $category = get_sub_field('sections-team-category');
    
    $args = array(
      'post_type'	=> 'team',
      'meta_query' => array(
        'name_clause' => array(
          'key' => 'tmi-categories_$_tmi-categories-category-name',
          'value' => $category,
          'compare' => '='
        )
      ),
      'meta_key'	=> 'tmi-categories_$_tmi-categories-order',
      'orderby'	  => 'meta_value_num',
      'order'		  => 'ASC'
    );
    
    $the_query = new WP_Query( $args );
    

    Note that the my_posts_where function comes from the docs and is just a workaround for not being able to use % in the query. I’ve only included it here for completeness and to explain the $ for people not familiar with the workaround.

    The best way I can sum up is: I need to order posts based on a repeater field row’s subfield value; the row containing the value is chosen by matching the other-subfield-in-the-row’s value with a variable. Specifically, order by tmi-categories-order in the repeater field row where tmi-categories-category-name matches $category.

    This is the closest post I was able to find on this issue but following the rabbit hole there didn’t get me anywhere (possibly because of user error though):
    https://support.advancedcustomfields.com/forums/topic/order-by-subfield-values/

    I do feel that the answer lies in setting orderby to an array and/or using the “clause” technique but I just don’t know how to set it up.

    Any help with this would be very much appreciated! Please let me know if I need to make this clearer.

    Thanks!!

  • It is pretty much impossible to do what you want to do. Matching one repeater row to another in a the where of an SQL query is not possible in any way. As for doing this for orderby is even more impossible simply because you’d be trying to order the posts by multiple different meta_key rows.

    To do this you would need to:
    1) Do your first query to get posts in the category<
    2) Loop through all the post and for each post
    3) Loop through the repeater to locate the row that matches the category
    4) when you find the row get the order value
    5) add a custom property to the post to hold the order
    6) Use something like php usort() to order the posts by the custom property that you added to each post

    I would not do this. First, I would not do a query based on a repeater sub field and second, the amount of work to order the posts is impractical.

    How would I do this?

    Before I go into that you should read this, you’ll need it to understand what I’m going to say next: https://acfextras.com/dont-query-repeaters/

    I would create a meta key and store all the values for tmi-categories in standard WP form. For example tmi-catetory-list. This meta key could have multiple entries in the database.

    For each category I would create another meta key, for example "tmi-category-order-{$categry-id}" and this meta key would hold the order for that specific catetory.

    With that done and working my meta query args would look something like the following

    
    $args = array(
      // all your other arguments, like post_type, posts_per_page, etc...
      'meta_query' => array(
        array(
          'key' => 'tmi-catetory-list',
          'value' => 'The category you want to list'
        )
      )
      'orderby' => "tmi-category-order-{$categry-id}"
    );
    

    PS: that topic you linked to is a very old one. In the 2+ years since then I discovered that it is impracticable to try to bend WP_Query to work with repeater fields and that it is much more piratical to bend the data to work with WP_Query

  • Thanks! That was exactly what I needed to get it done. I appreciate the thorough help!

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

The topic ‘Ordering posts by repeater subfield value, row chosen by associated subfield’ is closed to new replies.