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'
)
)
);
@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?
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.