0

I'm trying to query only the posts that have a post format of audio. I'm pretty sure I'm doing this wrong. Can I get a little help?

    <?php get_header();?>

    <section id="content">
        <section id="latest-blogs">

            <?php
                $i=1;
                $temp = $wp_query;
                $wp_query = null;
                $wp_query = new WP_Query();
                $wp_query -> query('post-format-audio'.'&paged='.$paged)
            ?>

            <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

                <div class="article-wrapper">
                    <span class="top-left-corner"></span>
                    <span class="top-right-corner"></span>
                    <article id="post-<?php the_ID(); ?>">
                        <span class="top-corners"></span>
                        <time datetime="<?php the_time('c'); ?>"><?php the_time('F j, Y'); ?></time>
                        <?php
                            $myvalue = get_field( "buzzsprout_shortcode" ); 
                            echo do_shortcode("$myvalue");
                        ?>
                        <p class="read-emails"><a href="<?php the_permalink() ?>" rel="bookmark" title="View emails and comment on this episode.">View emails and comment on this episode</a></p>
                        <span class="bottom-corners"></span>
                    </article>
                    <span class="bottom-left-corner"></span>
                    <span class="bottom-right-corner"></span>
                </div>

            <?php endwhile;endif; ?>

            <div id="pagination">
                <?php my_paginate_links(); ?>
            </div>

        </section>
    </section>

    <?php get_sidebar(); ?>

    <?php get_footer(); ?>

1 Answer 1

3

Before we continue, I must point out this:

            $i=1;
            $temp = $wp_query;
            $wp_query = null;
            $wp_query = new WP_Query();
            $wp_query -> query('post-format-audio'.'&paged='.$paged)

This is exactly the same as:

query_posts( 'post-format-audio'.'&paged='.$paged );

Which is terrible. When people advise against using query_posts, it's not just the function that's bad, it's what it does.

Thankfully someone told you about WP_Query, but they didn`t finish the job.

This is what your query loop should really look like:

$args = array(
    'post_format' => 'post-format-audio'
);
$q = new WP_Query( $args );
if ( $q->have_posts() ) {
    while( $q->have_posts() ) {
        $q->the_post();
        the_title(); // etc
    }
    wp_reset_postdata(); // clean up after ourselves
}

The important part being that post formats are saved as a custom taxonomy internally, with a taxonomy name of 'post_format'.

But even this is bad!

I saw in your original code you completely discard the original main query ( yet another reason query_posts is terrible ). What you should have done instead, is used the pre_get_posts filter to modify the main query before it runs, so that you don't have to discard it and waste time and slow down your page. This way also gives you all the pagination and other things that Core does for free.

So if this is your home page, you could do this:

function audio_only( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'post_format', 'post-format-audio' );
    }
}
add_action( 'pre_get_posts', 'audio_only' );

With that in a plugin or functions.php your homepage will only show posts with the audio post format. You can override individual term archives, searches, RSS feeds etc using this, anywhere that has a main loop.

But that's still not the best way of doing this. WordPress automatically gives you a post format archive for free! You can even use a special template file for it. So there's no need to build an audio format archive, there's already one there

Further Reading

Final Notes

  • echo do_shortcode("$myvalue"); is wasteful, there's no need for the quotes, use echo do_shortcode( $myvalue ); instead, it's a little faster to type and friendlier for editors and code intel
  • I know the codex uses it, but don't bother with if(): while() : the_post();, instead use brace brackets {} and move them on to their own lines. 1 statement/command per line. Again it's easier to read, and far nicer for automated tools and editors, and we all need all the help we can get so we can focus on the important stuff
  • All those corner span elements <span class="top-left-corner"></span> could be removed and replaced with multiple CSS backgrounds, border-radius, or :after and :before css selectors
1
  • 1
    Thank you so much for going in-depth and teaching me the ways!
    – Desi
    Commented Jul 14, 2014 at 4:32

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.