Support

Account

Home Forums General Issues Use ACF taxonomy to Query

Helping

Use ACF taxonomy to Query

  • I have a page that throws a loop of a custom post Type (stores), I created a taxonomy (store_the_genre) which is divided in Men, Women, Children for the type of clothing, I want to know how to use the Post Query to post the genre

    //Protect against arbitrary paged values
    $testMe = get_field('store_the_genre');
    $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
    						        if(have_posts() ):
       $args = array( 'post_type' => 'store',
    				  	'posts_per_page' => 11,
    				   'paged' => $paged,
    				  'meta_key' => 'store_client_type', 
    				  'tax_query' => array(
    						array(
    							'taxonomy' => 'style',
    							'field'    => 'slug',
    							'terms'    => $style,
    						),
    				  	array(
    							'taxonomy' => 'bgenre',
    							'field'    => 'term_id',
    							'terms'    => $testMe,
    						),   
    					),
    				 'meta_query' => array(
    					    'relation' => 'AND',
           					array(
               				'city_clause' => array(
                       						'key' => 'store_city', 
                       						'value' => $villeshow, 
    										'compare' => 'IN'
                							),
            							),
        							),	
    				 ); 
        $loop = new WP_Query( $args );
            while($loop->have_posts()):  $loop->the_post(); 
    
  • Possibly something like:

    <?php
    $testMe = get_field('store_the_genre'); //assume this is returning a term ID?
    
    global $wp_query;
    $paged = get_query_var('paged') ? get_query_var('paged') : 1;
    $args = array(
        'post_type' => 'store',
    	'posts_per_page' => 11,
    	'paged' => $paged,
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'style',
                'field'    => 'slug',
                'terms'    => $style,
            ),
            array( 
                'taxonomy' => 'store_the_genre',
                'field'    => 'term_id',
                'terms'    => $testMe,
            ),
    	
        ),
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key' => 'store_city',           
                'value'    => $villeshow,
    			'compare'    => 'IN',
            ),
    	
        ),
    );
    $query = new WP_Query( $args );		
     
    if( $query->have_posts() ) :
    	while( $query->have_posts() ): $query->the_post(); ?>
    	<p><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>	</p>
    	<?php endwhile;
    	wp_reset_postdata();
    else :
    	echo 'No stores found';
    endif;

    It depends if your store_the_genre taxonomy is a single value/multi value and returning the ID?

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

You must be logged in to reply to this topic.