Support

Account

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

  • 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