Support

Account

Home Forums Add-ons Repeater Field Taxonomy Filtering with Repeater Sub Fields

Solving

Taxonomy Filtering with Repeater Sub Fields

  • I’m having a lot of trouble here with my taxonomy filtering, hoping someone can help. I’ve read thru a ton of support discussions and ACF documentation but I’m just not getting it.

    I even found a super old post here, but I believe it’s been outdated?
    https://web.archive.org/web/20190814230622/https://acfextras.com/dont-query-repeaters/

    I have a taxonomy called “collect” with two taxonomies (“winter” and “summer), I have a repeater field called “hotspot_repeater” and a field inside that repeater called “hotspot_category” which I’ve set up with a Taxonomy field.

    On my taxonomy.php page where I want to show all posts with that specific taxonomy selected in the “hotspot_category” field.

    My site url setup is similar to this: site.com/collections/winter

    I’ve tried this 'value' => 'winter', I’ve tried the ID of the taxonmy value, which is 58. If I have the 'value' => 'winter', “No Posts” show. If I remove that, all the posts regardless of Taxonomy show.

    Am I missing something simple here or is there a better way to do this? @elliot

    My code is below:

    <?php
    
        // filter
        function my_posts_where($where)
        {
    
            $where = str_replace(
                    "meta_key = 'hotspot_repeater_$",
                    "meta_key LIKE 'hotspot_repeater_%",
                    $where);
    
            return $where;
        }
    
        add_filter('posts_where', 'my_posts_where');
    
        $args = array(
            'post_type' => 'shop',
            'posts_per_page' => -1,
            'suppress_filters' => false,
            'meta_query' => array(
                array(
                    'key' => 'hotspot_repeater_$_hotspot_category',
    //                'value' => 'winter', // or 58
                    'compare' => '='
                )
            )
        ); ?>
    
        <?php $search_query = new WP_Query($args); ?>
        <?php if ($search_query->have_posts()): ?>
            <?php while ($search_query->have_posts()) : $search_query->the_post(); ?>
                <?php echo the_title(); ?>
            
                <?php if (have_rows('hotspot_repeater')): ?>
    
                    <?php while (have_rows('hotspot_repeater')): the_row();
                    
                        $hotspot_title = get_sub_field('hotspot_title');
                        
                        ?>
    
                        <?php
                        $terms = get_sub_field('hotspot_category');
                        var_dump($terms); ?>
    
                        <!-- Item -->
                        <div class="mosaic-grid-item">
    
                            <!-- Wrapper -->
                            <div class="image-map-wrapper">
    
                                <?php echo the_title(); ?> --
                                <?php echo $hotspot_title; ?>
                                <?php
    //                            $terms = get_sub_field('hotspot_category');
    
                                if ($terms): ?>
                                    <?php echo $term; ?>
                                    <ul>
                                        <?php foreach ($terms as $term): ?>
                                            <h2><?php echo esc_html($term->name); ?></h2>
                                            <?php echo esc_html($term->description); ?>
    
                                        <?php endforeach; ?>
                                    </ul>
                                <?php endif; ?>
                            </div>
                        </div>
    
                    <?php endwhile; ?>
                
                <?php else : ?>
                    <?php // no rows found ?>
                <?php endif; ?>
    
            <?php endwhile; ?>
            <?php wp_reset_postdata(); ?>
        <?php else : ?>
            No result found.
    
        <?php endif; ?>
  • The post you linked to while old and needing to find it in the internet archive is still relevant and a good place to start.

    There simply isn’t any way to filter or order posts by the sub fields or a repeater. If you need to do this then you need to convert these values into something that can be used for this purpose as explained in that old article.

  • @hube2 Thanks for the reply. I ended up converting the taxonomy into categories and changing the taxonomy.php to a category-slug.php page. Like the code below. It seems to filter for the category. Only problem now is the pagination doesn’t work properly. Every page/* gives me a 404 that I can’t seem to figure out. I appreciate the response.

    
    $cat_id = get_query_var('cat');
    
            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
            
            $args = array(
                'posts_per_page'	=> 3,
                'post_type'		=> 'shop',
                'suppress_filters' => false,
                "meta_key" => "",
                "orderby" => "meta_value_num",
                "order" => "desc",
                "paged" => $paged,
                'meta_query' => array(
                    array(
                        'key' => 'hotspot_repeater_$_hotspot_category',
                        'compare' => 'LIKE',
                        'value' => array($cat_id),
                    )
                )
            );
    
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Taxonomy Filtering with Repeater Sub Fields’ is closed to new replies.