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.

Right now, I'm using get_posts to retrieve cusstom posts types with a custom taxonomy assigned to it in order to generate static content like this:

<?php
/**
 * Template Name: Front Page
 * @package WordPress
 * @subpackage Prominent
 * @since Prominent 1.0
 */
get_header(); ?>
<div class="shadow-top">
    <!-- Shadow at the top of the slider -->
</div>
<div id="intro">
    <div class="container">
        <div id="slider-wrapper">
            <div id="slider">
            <?php // Create custom loop ?>
            <?php $custom_posts = get_posts('post_type=page_content&page_sections=Slider (Front Page)'); ?>
            <?php foreach( $custom_posts as $post ) : setup_postdata( $post ); ?>
                <p><?php the_content(); ?></p>
            <?php endforeach; ?>
            <?php wp_reset_query(); ?>
            </div>
            <div class="shadow-slider">
                <!-- Shadow at the bottom of the slider -->
            </div>
        </div><!-- #slider-wrapper -->
    </div><!-- .container -->
</div><!-- #featured -->
<div class="shadow-bottom">
    <!-- Shadow at the bottom of the slider -->
</div>
<div id="tagline">
    <div class="container">
        <?php
        $page_id = $post->ID; // 123 should be replaced with a specific Page's id from your site, which you can find by mousing over the link to edit that Page on the Manage Pages admin page. The id will be embedded in the query string of the URL, e.g. page.php?action=edit&post=123.
        $page_data = get_page( $page_id ); // You must pass in a variable to the get_page function. If you pass in a value (e.g. get_page ( 123 ); ), Wordpress will generate an error.

        $content = apply_filters('the_content', $page_data->post_content); // Get Content and retain Wordpress filters such as paragraph tags. Origin from: http://wordpress.org/support/topic/get_pagepost-and-no-paragraphs-problem
        $title = $page_data->post_title; // Get title
        ?>
        <div class="content0">
            <h2><?php echo $content; // Output Content ?></h2>
        </div>
    </div><!-- .container -->
</div><!-- #content-bottom -->
<div id="content">
    <div class="container">
        <div class="mainbar">
            <?php // Create custom loop
            $custom_posts = get_posts('post_type=page_content&page_sections=Content (Front Page)'); ?>
            <?php foreach( $custom_posts as $post ) : setup_postdata( $post ); ?>
                <div class="content-block">
                    <h2><?php the_title(); ?></h2>
                    <p><?php the_content(); ?></p>
                </div><!-- .content-block -->
            <?php endforeach; ?>
            <?php wp_reset_query(); ?>
        </div><!-- #mainbar -->
    </div><!-- .container -->
</div><!-- #content-bottom -->
<?php get_footer(); ?>

What is better practice in this case?

To use query_posts, WP_Query, or get_posts?

enter image description here

share|improve this question
1  
possible duplicate of When should you use WP_Query vs query_posts() vs get_posts()? –  Jan Fabry Jan 29 '11 at 9:38

1 Answer 1

up vote 5 down vote accepted

Hi @janoChen:

If you have a choice, go with WP_Query. Both of the other functions (query_posts() and get_posts()) call WP_Query indirectly.

The former is designed to allow you to modify the main query after the standard query has already been run, for example when you want a second loop. But query_posts() affects global variables and can have side-effects. If possible use WP_Query instead and your code will be more robust.

As for get_posts(), it's just a wrapper around WP_Query with some potentially unexpected defaults so you might as well call WP_Query directly and avoid those issues.

share|improve this answer
    
Why is WP_Query the best choice in this situation? –  janoChen Jan 29 '11 at 6:28
    
@janoChen - I left a quick answer and then when back to fill in so I think I've answered your question. –  MikeSchinkel Jan 29 '11 at 6:32

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.