2

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'.

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();
?>
2
  • Why not use the in-built taxonomy archive? May 9, 2012 at 15:59
  • @Stephen - at the time I was trying to create a list of a custom taxonomy from within another template. If I remember this piece correctly, I had created a custom post type called 'Portfolio' for a freelancer's website. When you clicked through to the single-portfolio.php page, I also wanted to show the custom taxonomy for that specific post type, so users could navigate through other portfolio pieces by taxonomy (in this case, 'copywriting', 'seo writing', 'etc'; he
    – Rob Dobson
    May 11, 2012 at 9:47

1 Answer 1

0

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();
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.