Support

Account

Home Forums ACF PRO Get taxonomy custom field in WP_query

Solved

Get taxonomy custom field in WP_query

  • HI there, I have a custom colour picker field assigned to a taxonomy. The taxonomy is for a custom post type called ‘Callouts’. I’m displaying these callouts in a custom Gutenberg block on this page https://n-site.co.uk/ they are the coloured blocks in the carousel about half way down the page.

    It all works great, apart from the fact that I’d like to be able to display these blocks by ‘menu_order’. But for some reason I can’t figure out this is not happening. It seems to be ordering them by something to do with the taxonomy terms.

    Here’s the code I’m using in the Gutenberg block to display these callouts…

    <?php
    	$practiceareas = get_terms( array( 'taxonomy' => 'practice-area-callouts') );
    	foreach ( $practiceareas as $practicearea ) {
    		
    		$practice_query = new WP_Query( array(
                'post_type'       => 'callouts', // your custom post type
    			'posts_per_page'  => -1,
    			'orderby'		  => 'menu_order',
    			'order'           => 'ASC',
    			'tax_query'		  => array(
                    array(
    					'taxonomy' => 'practice-area-callouts',
    					'terms'    => $practicearea->term_id,
    				),
                )
            ) );
        ?>
    	<?php if ( $practice_query->have_posts() ) : ?>
    		<?php while ( $practice_query->have_posts() ) : $practice_query->the_post(); ?>
    			<div class="callout h-100 p-4 position-relative" style="background-color:<?php echo $colour;?>;">
    				<h4 class="has-white-color"><?php the_title(); ?></h4>
    				<?php the_content(); ?>
    				<?php 
    				$link = get_field( 'link', get_the_ID() );
    				if( $link ): 
    					$link_url = $link['url'];
    					$link_title = $link['title'];
    					$link_target = $link['target'] ? $link['target'] : '_self';
    				?>
    				<small><a class="stretched-link" href="<?php echo esc_url( $link_url ); ?>" target="<?php echo esc_attr( $link_target ); ?>"><?php echo esc_html( $link_title ); ?> <i class="fa fa-angle-right ms-1" aria-hidden="true"></i></a></small>
    				<?php endif; ?>
    			</div>
    		<?php endwhile; ?>
    	<?php endif;?>
        <?php $practice_query = null; wp_reset_postdata(); } ?>

    Any help would be greatly appreciated!

  • What is not being ordered correctly, the terms or the posts?

  • Hi John, it was the posts that weren’t being ordered correctly. But this post managed to help my resolve the issue https://stackoverflow.com/questions/50930583/displaying-custom-post-types-taxonomy

    Thanks!

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

You must be logged in to reply to this topic.