List custom posts by custom taxonomy

I’m trying to create a template which will read a tag from the page, then display all the custom posts which share that tag. So for example, I have a custom post type called ‘block’, and a taxonomy on the block which is called ‘appearson’, and a tag of that taxonomy called ‘products’.

I also have a products page, which shares the ‘appearson’ taxonomy, and also has the tag ‘products’.

Read More

I want the products page template to read its appearson tag, then find the custom posts associated with that tag.

This is what I’m trying:

<?php $terms = get_the_terms( $post->id, 'AppearsOn' );?>


<?php
$args = array(
'post_type' => 'block'
'tax_query' => array(
array(
        'taxonomy' => 'appearson',
        'field' => 'id',
        'terms' => '$terms'
    )
  )
); ?>

<?php $the_query = new WP_Query( $args ); ?>

<?php while ( $the_query->have_posts() ) : $the_query->the_post();?>

It’s not happy with this. Any pointers as to what I’m doing wrong? Blindingly obvious, I’m sure, but my 4 month old baby girl has stolen my brain.

Many thanks

Rob

EDIT

Actually ignore some of that. I’m now doing this:

<?php $terms = get_the_terms( $post->id, 'appearson' );?>


<?php
query_posts( array( 'post_type' => 'block', 'appearson' => $terms->name ) );
if ( have_posts() ) : while ( have_posts() ) : the_post();
?>

So the bit I’m stuck on is getting ‘appearson’ => $terms to work. Any idea?

ta

Rob

Final edit:

Sorted it:

<?php $blocktaxonomy = wp_get_object_terms($post->ID, 'appearson');
$blocktaxonomy  = $blocktaxonomy [0];
query_posts( array( 'post_type' => 'block', 'appearson' => $blocktaxonomy ->name ) );

if ( have_posts() ) : while ( have_posts() ) : the_post();
?>

Related posts

Leave a Reply

1 comment

  1. Solution:

    <?php $blocktaxonomy = wp_get_object_terms($post->ID, 'appearson');
    $blocktaxonomy  = $blocktaxonomy [0];
    query_posts( array( 'post_type' => 'block', 'appearson' => $blocktaxonomy -    >name ) );
    
    if ( have_posts() ) : while ( have_posts() ) : the_post();
    ?>