Support

Account

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

Solved

Only list custom post types that match sub field taxonomy

  • I have a custom post type (Colour Swatches) The post type allows you to add a colour and allocate it a category in a repeater. I am listing the post types on a page along with the colours that match a specific category.
    Listing the correct colours or each post type is working, this uses a for each loop querying the category sub field. However if no colours match the query the title still appears. I would like the page not to be listed at all if any of the sub fields do not match the category.
    I assume the best way to achieve this is through a meta query, but I am not sure where to start with this.

    The code below is as far as I’ve got..

    <?php 
    $args = array(
        'post_type'     => 'colourswatch',
        'order'       => 'ASC',
        'meta_query' => array(
            array(
                // no idea
            )
        )
    );
    $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">
            
            <?php if( have_rows( 'colour_swatch' ) ): ?>
            // I need to hide this if the category does feature for any colours
            <h3 class="swatches__title"><?php the_title(); ?></h3>  
            <ul class="swatches__colours">
                <?php while ( have_rows( 'colour_swatch' ) ) : the_row();
                
                $terms = get_sub_field( 'availability' );            
                foreach( $terms as $term ) {
                    if ( $term->name == 'windows' ) : ?>
                    <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 endif; ?>
                <?php } ?>
                <?php endwhile; ?>
            </ul>
            
            <?php endif; ?>
        </div>
        <?php endwhile; ?>
    </div>
    <?php endif; wp_reset_query(); ?>
  • @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?

  • I think I have solved it.

    From what I understand for a taxonomy the value mst be the ID rather than the name or slug or whatever, so since I need to use the name I need to convert that to the ID. I found that I could do this using get_term_by();

    This is the portion of code I needed to change:

       // Get term by name 'windows' in custom taxonomy 'product-types'.
        $termId = get_term_by( 'name', 'windows', 'product-types' );
        
        // compose our query arguements
        $args = array(
            'post_type'     => 'colourswatch',
            'meta_query'    => array(
                array(
                    'key'       => 'colour_swatch_$_availability', // repeater_$_subfield
                    'value'     => $termId->term_id, // translate the term name into its ID
                    'compare'   => 'LIKE'
                )
            )
        );
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.