Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've been trying to utilize either WP_Query or get_posts to pull out the most recent post that's both in a specific category and of a specific post format.

<?php 
$singargs = array(
'numberposts' => 1,
'tax_query' => array(
    'relation' => 'AND',
    array(
        'taxonomy' => 'category',
        'field' => 'slug',
        'terms' => array ( 'gift-of-the-day' ),
    ),
    array(
        'taxonomy', => 'post_format',
        'field' => 'slug',
        'terms' => array( 'aside'),
    )
)
);
$singPost = new WP_Query( $singargs );
foreach ( $singPost as $post ) : setup_postdata($post); ?>
<aside>
    <h2><?php the_title(); ?></h2>
</aside>
<?php endforeach; wp_reset_postdata(); ?>

What am I doing wrong here?

share|improve this question
    
Well, the foreach is wrong. That's not a proper Loop. You should be doing while ($singPost->have_posts()) : $singPost->the_post(); instead –  Otto Aug 31 '11 at 19:18
    
Also, aside is not a proper post format taxonomy slug, so you won't get any results in this query. The correct one to use there is post-format-aside. –  Otto Aug 31 '11 at 19:19
    
I had tried post-format-aside prior to asking, but had no success. I also tried swapping out the foreach for the while loop syntax you suggested, with no success. (Leaving post-format-aside of course.) –  jaacob Sep 2 '11 at 1:59

2 Answers 2

Two problems i see: change aside to post-format-aside and since you are using foreach loop change new WP_Query( $singargs ); to get_posts( $singargs ); so your code sould look like this:

<?php 
$singargs = array(
'numberposts' => 1,
'tax_query' => array(
    'relation' => 'AND',
    array(
        'taxonomy' => 'category',
        'field' => 'slug',
        'terms' => array ( 'gift-of-the-day' ),
    ),
    array(
        'taxonomy', => 'post_format',
        'field' => 'slug',
        'terms' => array( 'post-format-aside'),
    )
)
);
$singPost = get_posts( $singargs );
foreach ( $singPost as $post ) : setup_postdata($post); ?>
<aside>
    <h2><?php the_title(); ?></h2>
</aside>
<?php endforeach; wp_reset_postdata(); ?>
share|improve this answer
    
I've tried this code verbatim, using the get_posts() and foreach. It didn't work. I also tried it with @Otto's suggestions of using a while loop with the syntax he suggested. That also did not work. –  jaacob Sep 2 '11 at 1:57
1  
There's nothing wrong with this code. Perhaps you don't have any posts that match? –  Otto Sep 2 '11 at 16:02
    
I have a post that is in the category with a slug "gift-of-the-day", that is set to the post format "aside", that is published. –  jaacob Sep 8 '11 at 5:27

Why don't just use something like :

$args = array('category' => 1 );
$all_posts = get_posts( $args);
foreach ($all_posts as $this_post) {
    if ( has_post_format( 'aside' ) ) {
        echo '<aside><h2>' . $this_post->title . '</h2></aside>';
        break;
        }
}

Could be some mistake in the code, but the idea is to first get the posts in wanted category, then parse it and take the first post that has the wanted post format (in case you couldn't make your code work).

Sorry if i'm wrong !

share|improve this answer

Your Answer

 
discard

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.