Support

Account

Home Forums General Issues Query posts via Taxonomy field

Solved

Query posts via Taxonomy field

  • I have an ACF taxonomy field that gets multiple terms from a custom taxonomy. I’m attempting to get and list the posts assigned these terms. Seems logically accomplishable, but for some reason i’m missing the method.

    I’ll break it down with an example:

    • I have a page with a book on it. Moby Dick.
    • I have a CPT called “Quotes”
    • In this CPT i have a custom Tax called “Topics”- with (for instance) custom terms including “Whales” and “eyepatches”
    • On the books’ edit page i have an ACF Taxonomy field set to the “Topics” custom Taxonomy. I select both the “Whales” and “eyepatches” tax terms (as they are relevant to Moby Dick.)
    • I then have a query at the bottom of the Moby dick page, attempting to query the ACF Taxonomy selector, for those tax terms, and then using those tax terms, pull at random 5 posts from “quotes” CPT via those given custom Tax terms.
  • To get the tax terms for that page you can use this:
    https://codex.wordpress.org/Function_Reference/get_the_terms

    Then plug this into a get_posts

    get_posts(array(
        'post_type' => 'quotes',
        'tax_query' => array(
            // get_the_terms() stuff.
        ),
        'order' => 'ASC',
        'posts_per_page' => 5,
        'orderby' => 'rand')
    );
    ?>

    Note: This is off the top of my head but I hope it sets you on the path to your answer 🙂

  • So what you need to do is get the ACF field, which will return an array of term IDs and use that array to do a WP_Query, similar to what magicstick posted.

    For this you need to set the taxonomy field on the book edit page to return Term ID, if it’s returning Term Object it will be a bit more complicated.

    
    $topics = get_field('topics');
    if ($topics) {
      if (!is_array($topics)) {
        $topics = array($topics);
      }
      $args = array(
        'post_type' => 'quotes',
        'posts_per_page' => 5,
        'orderby' => 'rand',
        'tax_query' => array(
          array(
            'taxonomy' => 'topics',
            'terms' => $topics,
          ),
        ),
      );
      $quote_query = new WP_Query($args);
      if ($quote_query->have_posts()) {
        while($quote_query->have_posts()) {
          $quote_query->the_post();
          echo '<p>',get_the_content(),'</p>';
        }
      }
      wp_reset_postdata();
    }
    
  • This is great – thanks so much for the help. I was able to get things working with this assist!

  • Hi,
    i have a similar problem:

    I have a custom post type with different taxonomies.

    I want to check for a custom field within a site, which holds the taxonomy name from the custom posts. So i can enter the taxonomy in an ACF field on top of each site, and the loop should output all posts within that taxonomy i’ve entered. But it seems not to work, and my coding skills are poor…

    “kategorie_eintragen” is my taxonomy ACF field for the sites, i’ve added these taxonomies before within my custom post type “touren”.

    Here my code from the page template:

    					<div class="videos">
    
    						<?php 
    
    						// get posts
    						$kategorie = get_field('kategorie_eintragen');
    						$posts = get_posts(array(
    							'post_type'			=> 'touren',
    							'posts_per_page'	=> -1,
    							'post_status' 		=> 'publish',
    							'orderby' 			=> 'date',
    							'order'				=> 'ASC',
    							'tax_query' => array(
    								array(
    									'taxonomy' => 'kategorie_eintragen',
    									'terms' => $kategorie,
    								)
    							)
    						));
    
    						if( $posts ): ?>
    
    							<?php foreach( $posts as $post ): 
    
    								setup_postdata( $post ) ?>
    						
    								<article id="post-<?php the_ID(); ?>" class="post-<?php the_ID(); ?> post type-post status-publish format-standard">
    								<a href="<?php the_field('link_zur_tour'); ?>">
    								<div class="videos-title aktiv-bild">
    									<?php 
    									$image = get_field('bild_der_tour');
    									$size = 'oberkategorien'; // (thumbnail, medium, large, full or custom size)
    									if( $image ) {
    										echo wp_get_attachment_image( $image, $size );
    									} ?>
    									<h2><?php the_field('titel_der_tour'); ?></h2>
    								</div>
    								</a>
    								</article>
    						
    							<?php endforeach; ?>
    
    							<?php wp_reset_postdata(); ?>
    
    						<?php endif; ?>		
    						
    					</div>

    How do i make the loop query these custom post types only within the taxonomy i gave him on the current site?

  • Nevermind, i’m dumb… solved it:

    						$kategorie = get_field('kategorie_eintragen');
    						$the_query = new WP_Query( array(
    							'post_type'			=> 'touren',
    							'posts_per_page'	=> -1,
    							'post_status' 		=> 'publish',
    							'orderby' 			=> 'date',
    							'order'				=> 'ASC',
    							'tax_query' => array(
    								array(
    									'taxonomy' => 'touren-categories',
    									'field' => 'slug',
    									'terms' => $kategorie
    								)
    							)
    						));
  • Is there a way to display the results of this query separated by the taxonomy that you used in the acf field?

    Taxonomy 1
    -Post A1
    -Post A2

    Taxonomy 2
    -Post B1
    -Post B2

  • “Is there a way to display the results of this query separated by the taxonomy that you used in the acf field?”

    NM, I figured it out

  • What was the solution ? I am trying to do something similar.
    Instead of subcategories displaying a “-” I want it to have the parent.

    Example:

    Taxonomy 1
    – (Taxonomy 1) Post A1
    – (Taxonomy 1) Post A2

    Taxonomy 2
    – (Taxonomy 2) Post B1
    – (Taxonomy 2) Post B2

  • Hi there — would you be able to post this snippet of code? I’m stuck on this exact same issue and at my wit’s end. Any help would be appreciated!

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

The topic ‘Query posts via Taxonomy field’ is closed to new replies.