Support

Account

Home Forums Front-end Issues Select field filter not working

Solved

Select field filter not working

  • Hello ACF and users,

    I’m running into a slight problem with filtering on a select field. I’ve created a custom post type called ‘leden’ and assigned a ‘leden_details’ repeater field with multiple sub fields to that post type.

    I’ve read your documentation and I’m using the code you give as example(s). Even tried multiple other variants but it keeps returning blank.

    This is the code I use ;

    <?php 
     
                        $posts = get_posts(array(
                            'post_type'     => 'leden',
                            'posts_per_page'    => -1,
                            'meta_key'      => 'plaats_lid',
                            'meta_value'        => 'gytsjerk'
                        ));
                         
                        if($posts)
                        {
                            foreach($posts as $post)
                            {
                                echo'<li>'.the_title().'</li>';
                            }
                        }
    
                    ?>

    The meta_key is the sub select field where the post should filter on. The meta_value is one of 7 options that can be given to every new post on the ‘leden’ post type.

    Does anyone know why it keeps returning blank?

    If I delete the meta_key and meta_value it is working properly and returning all post from the post type.

    Hope you can help me out.

    Thanks!

    – Justin

  • Hi @jusseb

    You mentioned that the select field is a sub field?
    It is not possible to query a sub field with the code above. Please read this documentation to learn more:
    http://www.advancedcustomfields.com/resources/tutorials/querying-the-database-for-repeater-sub-field-values/

    Thanks
    E

  • Thanks, that worked like a charm!

  • I have another question regarding this topic.

    I’ve managed to get it working with the help of your documentation. It shows the post title of my entries.

    But how do I show a custom field within the same loop?

    This <?php echo get_the_title( $row->post_id ); ?> shows the title, but how do I show a custom field?

  • Hi @jusseb

    You can load a custom field from another post by adding in the $post_id parameter like so:

    
    echo get_field('field_name', $row->post_id);
    

    Thanks
    E

  • Hi Elliot,

    Thanks for your response.

    I’ve tried it like this ;

    <?php echo get_sub_field('adres_lid', $row->post_id); ?>

    But it returns empty. Or isn’t it possible to show a subfield like that?

    This is the whole code now

    <?php
         
                            // get all rows from the postmeta table where the sub_field (type) equals 'type_3'
                            // - http://codex.wordpress.org/Class_Reference/wpdb#SELECT_Generic_Results
                                $rows = $wpdb->get_results($wpdb->prepare( 
                                    "
                                    SELECT * 
                                    FROM wp_postmeta
                                    WHERE meta_key LIKE %s
                                        AND meta_value = %s
                                    ",
                                    'leden_details_%_plaats_lid', // meta_name: $ParentName_$RowNumber_$ChildName
                                    'gytsjerk' // meta_value: 'type_3' for example
                                ));
                         
                            // loop through the results
                            if( $rows )
                            {
                                foreach( $rows as $row )
                                {
                                   
                         
                            ?>
    
                                <li class="leden-details"><?php echo get_the_title( $row->post_id ); ?></li>
                                <li class="leden-show"><?php echo get_sub_field('adres_lid', $row->post_id); ?></li>
    
                        <?php
                     
                            }
                        }
                     
                        ?>

    Thanks

  • Hi @jusseb

    The function get_sub_field will not work within your custom $wpdb->get_results loop. It only work inside a have_rows or has_sub_field loop while loop.

    Perhaps the data already exists inside $row?

    Try debugging it like so:

    
    <?php echo '<pre>';
    	print_r($row);
    echo '</pre>';
    die; ?>
    

    Thanks
    E

  • Hi Elliot,

    It seems the data doesn’t already exist inside $row.

    This is the output ;

    stdClass Object
    (
        [meta_id] => 1301
        [post_id] => 144
        [meta_key] => leden_details_0_plaats_lid
        [meta_value] => gytsjerk
    )

    What is the best solutions to output the subfield in the $wpdb->get_results loop?

    Hope to hear from you,

    Thanks!

  • Hi @jusseb

    I believe the $row->meta_value will contain the value of the queried sub field

    Thanks
    E

  • Hi Elliot,

    So I have to output it like so?

    <?php echo get_sub_field('adres_lid', $row->meta_value); ?>

    Because that returns empty.

    Sorry to bother you with so many questions.

  • Hi @jusseb

    No. The get_sub_field function can be used only within a have_rows or has_sub_field while loop. You have a custom loop, so you can’t use this function.

    Instead, you can find your value simply with this code:
    $value = $row->meta_value;

    Thanks
    E

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

The topic ‘Select field filter not working’ is closed to new replies.