4

I have the following code from a template to view get the post information:

<?php
 $args = array(
    'post_type'      => 'post',
    'posts_per_page' => $count,
    'paged'          => $paged,
);
$wp_query = new WP_Query( $args );
?>

<?php if ( $wp_query->have_posts() ) : ?>
    <?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
        <?php echo $wp_query->post_title.'<br>';
    <?php endwhile; ?>
<?php endif; ?>

I'd like to get display the post title etc in here but when I tried to do it like above it didn't display anything. How can I access the post information?

5

First of all, don't use and abuse the $wp_query global variable. This global should be reserved to the main query only. Use any other variable that will not create conflict.

Secondly, don't use the raw WP_Post properties. These are raw and unfiltered. WP_Query does set up postdata by default which make the use of the template tags possible.

Thirdly, always reset all instances of WP_Query to avoid any conflict with other queries on the same page

Fourthly, you have a syntax error in this line <?php echo $wp_query->post_title.'<br>';. You are missing a closing php tag

You can rewrite your code to something like this

 $args = array(
  'post_type'      => 'post',
  'posts_per_page' => $count,
  'paged'          => $paged,
);
$query = new WP_Query( $args );

if ( $query->have_posts() ) {
    while ( $query->have_posts() ) { 
    $query->the_post();
        the_title();
        the_content();
    }
    wp_reset_postdata();
}

EDIT

If you need to access the WP_Post properties directly, you can use for instance

echo apply_filters( 'the_content', $query->post_content );

to display the post content,

echo apply_filters( 'the_title', $query->post_title );

will display the post title. The post status can be accessed by

$query->post_status

See a list of all available properties here

You have to remember, there are template tags that are available during the loop to display these info without using the WP_Post properties

2
  • I asked the same question below: What if I want to access more things than that, like the post_status and things like that? Is not using the WP_Query like that a good idea for that? Jan 29 '15 at 19:15
  • See my update. Hope this helps Jan 29 '15 at 19:22
0

Slight error in your code. Once you've begun your loop (which you do after setting $wp_query->the_post();) then you can begin to access content from your post using WordPress template tags. So if you wanted to show a title and content it would look like this:

<?php
$args = array(
  'post_type'      => 'post',
  'posts_per_page' => $count,
  'paged'          => $paged,
);
$wp_query = new WP_Query( $args );
?>

<?php if ( $wp_query->have_posts() ) : ?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<h3><?php the_title(); ?></h3>
<p><?php the_content(); ?></p>
<?php endwhile; ?>
<?php endif; ?>

I'd recommend reading up on WP_Query and template tags.

2
  • What if I want to access more things than that, like the post_status and things like that? Jan 29 '15 at 19:12
  • 1
    Those are all done with template tags, see the link above. <?php the_author(); ?> would output the author name, <?php echo get_post_status(); ?> will give you the post status, etc. Jan 29 '15 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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