Support

Account

Home Forums Front-end Issues Font-end search form for a subfield Reply To: Font-end search form for a subfield

  • Searching sub field by sub field. That would be quite a complex meta query.

    The first thing you’d need to know is the maximum number of rows that any post my have. So you’d need to loop through all the post once just to get that value. A example of this would be

    
    // get all posts in the post type, then loop through them
    $max_rows = 0; // will hold the maximum number of rows
    $repeater = 'name_of_repeater_field'
    while (have_posts()) {
        the_post();
        $count = intval(get_post_meta($post->ID, $repeater, true));
        if ($count > $max_rows) {
            $max_rows = count;
        }
    }
    
    // now that we know the number of rows
    // dynamically generate meta query
    // this uses nested meta queries introduced in WP 4.1
    $meta_query = array(
        relation => 'OR'
    );
    $subfield1 = 'subfield1'; // name of first subfield
    $subfield2 = 'subfield2'; // name of second subfield
    for ($row=0; $row<$max_rows; $row++) {
        $row_query = array(
            'relation' = 'AND',
            array(
                'key' => $repeater.'_'.$row.'_'$sufield1,
                'value' => 'value to search for'
            ),
            array(
                'key' => $repeater.'_'.$row.'_'$sufield2,
                'value' => 'value to search for'
            ),
        );
        $meta_query[] = $row_query;
    }
    

    Hope that helps