Support

Account

Home Forums General Issues WP Query based on whether repeater has rows or not

Helping

WP Query based on whether repeater has rows or not

  • Hi

    I have a custom post which essentially contains a repeater for special offers relating to that post. I then have a page running a custom query that pulls in items of this post type, but i want it restricted to only those which have rows in the special offers repeater.

    I realise I could pull in all the posts regardless of the repeater status, and then do a separate check for those rows and only display the ones that have them – but this messes up the pagination because as far as it is concerned there are as many results as there are posts – it doesn’t care whether they have repeater rows or not. I could have 1000 posts of this type and only one that has a special offer row. Doing it this way would show 1000 posts worth of pages in the pagination, but just 1 result in the actual results section.

    So I need to do this check at the wp_query level, is there any way?

    Thanks

  • The repeater field holds an integer representing the number of rows. The meta query would look something like this

    
    $meta_query = array(
      'relation' => 'AND',
      array(
        'key' => 'your-repeater-field-name',
        'compare' => 'EXISTS'
      ),
      array(
        'key' => 'your-repeater-field-name',
        'value' => 0,
        'type' => 'NUMERIC'
        'compare' => '>'
      ),
    )
    

    the meta_key exists and it’s value is > 0

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

The topic ‘WP Query based on whether repeater has rows or not’ is closed to new replies.