Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

here's what i'm trying to do.

  • i have a custom post type (CPT) which has custom taxonomies (web, print, video, custom).
  • each custom post has a custom taxonomy associated with it, as well as a featured image.
  • i am creating a grid of the featured images (basically a gallery) for each taxonomy.
  • i have a page, called design that calls the different taxonomies based on user interaction.
  • the hierarchy is ~/design/taxonomy (web, print, video, custom)

i have code that will display the images, but currently only for a specific taxonomy, by using this code

<ul class="feat-imgs">
<?php $loop = new WP_Query( array( 'package_types' => 'web', 'posts_per_page' => 6 ) );
while ( $loop->have_posts() ) : $loop->the_post();
 echo '<li>';the_post_thumbnail($loop->ID);
 echo '</li>';
endwhile; ?>
</ul>

can anyone help me modify this code to get the custom loop to grab the taxonomy ID in this line

$loop = new WP_Query( array( 'package_types' => 'web', 'posts_per_page' => 6 ) );

instead of having to hardcode the 'package_types' => 'web'??

it might also be worth noting that this code sits inside a standard Loop.

any help is appreciated.

share|improve this question

closed as too localized by toscho Feb 18 at 23:38

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

1 Answer

You could use a taxonomy query to check against the taxonomy id instead of the slug.

<?php
$args = array(
'tax_query' => array(
    array(
        'taxonomy' => 'package_type',
        'field' => 'id',
        'terms' => 3
    )
)
);
$query = new WP_Query( $args );
?>

Check out Taxonomy Queries for more examples

share|improve this answer
1  
that seems to break the entire page, now it just loads a blank page. also, what does the 'terms' => 3 signify? – jedifunk Nov 8 '11 at 20:34
seems to me that it needs some other argument to capture the taxonomy ID. maybe something like this (which doesn't work, btw) $taxID = get_post_meta($post->ID, 'package_types_id', true); any thoughts? – jedifunk Nov 8 '11 at 20:55
The terms => 3 is the term id of the term you are trying to get the posts for. To get the term id of a term via the slug (web) you can use the get_term_by() function (Which documentation for it can be found here codex.wordpress.org/Function_Reference/get_term_by). The code I gave you was not meant to just be copied and pasted as that is not what I was doing. I was just pointing you in the right direction and you should change the code as needed to fit your situation. – Kevin Langley Jr. Nov 8 '11 at 21:03
yes, thank you kevin. first time using CPT & custom taxonomy, so i'm a little confused. i'll give this a shot and see what i can do. – jedifunk Nov 8 '11 at 21:04
ok, i'm really sorry all, but i'm totally lost. can anyone help me out with a fairly "cut/paste" option to this question. i just don't understand how to use get_term to grab the taxonomy ID, which can they be used dynamically to display the featured image to just that taxonomy. any help is appreciated from this very confused person. – jedifunk Nov 8 '11 at 21:49
show 1 more comment

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