WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it possible to retrieve post entry for a custom post type by tag, I have been trying with the following code, however it just locks me into a infinte loop.

<aside class="supporting_images">
    <?php /*<?php if($post->ID == 241) : echo apply_filters('the_content', '[slideshow=3]'); endif;  ?>
    <?php the_post_thumbnail(); ?>*/?>

    <?php if($post->ID == 241) : ?>
            $query = new WP_Query();
        <?php while ($query->have_posts()) : ?>
        <?php endwhile; ?>
    <?php endif;?>

share|improve this question
up vote 0 down vote accepted

You'll need to setup the post for the query by changing the following line to get rid of the infinite loop.

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

If your looking for a custom post type, you'll need to specify that in the query arguments:

<?php $query = new WP_Query( array( "post-type" => "yourposttype", "tag" => "branding" ) ); ?>

You can see most (if not all) of the query parameters in the codex. http://codex.wordpress.org/Class_Reference/WP_Query#Parameters

share|improve this answer

To avoid infinite loop, you need to loop over resulting posts:

$query = new WP_Query(
    array( "post_type" => "yourposttype", // not "post-type" !
           "tag" => "branding"
    ) );
while ($query->have_posts()) : $query->the_post(); ?>
<?php endwhile; ?>
share|improve this answer

Use tag_slug__in not tag (#REF)

Example :

$query = new WP_Query(array(
   'post_status' => 'publish',
   'post_type' => 'your-posttype',
   'tag_slug__in' => 'your-tag',
   'posts_per_page' => -1

Step : Filter by Post-type then filter by Tag

Another answers "Filter by Post-type and filter by Tag"

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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