Support

Account

Home Forums Add-ons Repeater Field Query posts based on two subfields displays extra post without these values

Solving

Query posts based on two subfields displays extra post without these values

  • Hello, I’ve got the following problem while querying with repeater subfields:
    My aim is to query posts where the subfield ‘age’ (leeftijd) (which is a radio button) is ‘4-6’, ‘7-9′, ’10-12’, or ‘4-12’.
    Then it should also query whether it has a subfield ‘subject’ (onderwerp) (which are checkboxes), like ‘inrichten’, ‘communicatie’, and ‘regels’.
    So for example I have a post where the age is ‘7-9’, and the subjects in that row are ‘inrichten’ and ‘communicatie’. It also has ‘age’: ‘4-6’ and ‘subject’: ‘regels’ (which is why I use a repeater field for this).
    It should show the post whether I query on ‘inrichten’ or ‘communicatie’.
    I’ll use the following query (with ‘inrichten’):

    <?php function my_posts_where( $where ) {
    
        $where = str_replace("meta_key = 'gegevens_%", "meta_key LIKE 'gegevens_%", $where);
    
        return $where;
    }
    
    add_filter('posts_where', 'my_posts_where'); ?>
    
    <?php $args = array(
        'post_type'         => 'spelvormen',
        'post_status'       => 'public',
        'posts_per_page'    => '-1',
        'order'				=> 'ASC',
        'orderby'			=> 'title',
        'meta_query'        => array(
            'relation'		=> 'AND',
            array(
                'key'		=> 'gegevens_%_leeftijd',    //radio
                'compare'	=> '=',
                'value'		=> '7-9',
            ),
            array(
                'key'		=> 'gegevens_%_onderwerp',    //checkbox
                'compare'	=> 'LIKE',
                'value'		=> 'inrichten',
            ),
        ),
    );
    
    $query = new WP_Query( $args ); ?>

    This works, it shows a post which has both values. The problem, however, I have two more posts (a total of 3), where the third post doesn’t have the subject ‘inrichten’ (where age is ‘7-9’).
    But it is still being shown in the frontend, while it shouldn’t. Did I do something wrong in my query? I’m using this for a ajax form where people can select the age and the subject, and then it should display posts which have these values.
    The form works, but as I said before, it displays an extra post which should’nt be displayed.

  • Hi @dejong12

    Could you please share the JSON export file of your field group so I can test it out on my installation? Also, could you please share the screenshots of the backend so I know the data I should set for each post?

    Thanks 🙂

  • Sure, here is the JSON file: http://pastebin.com/2FFJ5up3.
    And the data for the posts: http://imgur.com/a/oI9k4

  • Do you need some extra input or are the files I posted here sufficient?

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

The topic ‘Query posts based on two subfields displays extra post without these values’ is closed to new replies.