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

I am trying to make use of the Single template for single posts ( I have a custom one). But for some reason my posts get overridden instead of displaying new data.

This is the setup I have right now

<div class="details">
<?php global $post;

$announcements = new WP_Query(); 
while ( $announcements->have_posts() ) :  $announcements->the_post(); ?>  

    <p><strong>Published by:<span itemprop="publisher"><?php echo 
get_post_meta($post->ID, 'Publisher','true');?></strong></p>

<?php endwhile;?>
share|improve this question
Why anyway are you doing a second query. – kaiser Jul 11 '13 at 10:46

But for some reason my posts get overridden instead of displaying new data.

The problem is here: $announcements->query('showposts=1&cat=$category_id');

You are trying to use a variable inside a single quote string. Variables do not expand inside single quotes so instead of asking for cat=1, or cat=2, you are literally asking for cat=$category_id-- just like that, spelled out in full.

What you need instead are double quotes: $announcements->query("showposts=1&cat=$category_id");. Variables do expand in double quote.

Or better, an array-- far more readable and maintainable:

$args = array(
  'posts_per_page' => 1,
  'cat' => $category_id,

Note: showposts is deprecated. Use posts_per_page.

share|improve this answer

You need to Pass arguments into the new Object like so.

$category_id = get_the_category( $post->ID );

$args = array(
    'posts_per_page' => 1,
    'category'       => $category_id

$announcements = new WP_Query( $args ); 

This page should shine some light on your code for you, I think you've got a few typo's within it and how you call the loop is in the slightly wrong order. WP_Query in Codex.

share|improve this answer

I tried that, but it still overrides for instance

the_title(), get_post_meta($post->ID, 'key', 'true'), the_post_thumbnail(),

and pretty much all of them gets overriden(What I mean is when I make two posts using the same template, the fields, titles and images get overridden.) Here is how I did it:

$category_id = get_the_category( $post->ID ); $args =  

array('posts_per_page' => 1, 'category' => $category_id); $announcements = 
new WP_Query( $args );

while($announcements->have_posts()): $announcements->the_post();

<div class="cover"><?php the_post_thumbnail(); ?></div>
<p><strong><span itemprop="title"><?php the_title();?></span></strong></p>

<?php endwhile;?>
share|improve this answer
Answer for @kaiser below. – Johnny Hankgard Jul 11 '13 at 12:01
Answer are not threaded, @Johnny. – Johannes Pille Jul 11 '13 at 13:28

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.