Support

Account

Home Forums Front-end Issues Only list custom post types that match sub field taxonomy Reply To: Only list custom post types that match sub field taxonomy

  • @vipstephan This has been of help, thank you. With a little bit of experimentation I’ve been able to successfuly do a query based on a sub field that is a basic text field value but all attempts at doing the same for a custom taxonomy – which I was hoping to do – have so far failed.

    In my updated code below the taxonomy is ‘availability’ and the taxonomy name I’m aiming to filter to in this case is ‘windows’.

    In functions:

    function my_posts_where( $where ) {
    	$where = str_replace("meta_key = 'colour_swatch_$", "meta_key LIKE 'colour_swatch_%", $where);
    	return $where;
    }
    add_filter('posts_where', 'my_posts_where');

    and on my template:

    <?php
    
        $args = array(
            'post_type' => 'colourswatch',
            'meta_query' => array(
                array(
                    'key' => 'colour_swatch_$_availability',
                    'value' => 'windows',
                    'compare' => 'LIKE'
                )
            )
        );
        $query = new WP_Query( $args );
        
        if ( $query->have_posts() ) : 
        ?>
        <div class="container container--narrow swatches">
            <?php while ( $query->have_posts() ) : $query->the_post(); ?>
            <div class="swatches__material">
    
                <h3 class="swatches__title"><?php the_title(); ?></h3>
    
                <?php if( have_rows( 'colour_swatch' ) ): ?>
                <ul class="swatches__colours">
                    <?php while ( have_rows( 'colour_swatch' ) ) : the_row(); ?>
                    <li class="swatches__swatch">
                        <div class="swatches__colour" style="background:<?php the_sub_field( 'colour' ); ?>"></div>
                        <h4 class="swatches__name"><?php the_sub_field( 'colour_name' ); ?></h4>
                    </li>
                    <?php endwhile; ?>
                </ul>
    
                <?php endif; ?>
            </div>
            <?php endwhile; wp_reset_postdata(); ?>
        </div>
        <?php endif; ?>

    What am I missing?