wp query - Why is this coming back as null? Thats wrong. There is one post

I have a category, called events. It is assigned to one post. So I created the following loop:$args = array ('categ

I have a category, called events. It is assigned to one post. So I created the following loop:

$args = array ('category_name' => 'events', 'posts_per_page' => 4, 'orderby' => 'date', 'order' => 'DESC' );
$category_posts = new WP_Query($args);

if ($category_posts->have_posts()) {
  while ($category_posts->have_posts()) {
    $category_posts->the_post();
    var_dump($category_posts->the_post());

    ?>
      <div class="col-md-6">
        <?php $category_posts->the_post_thumbnail(array('class' => 'events-image')); ?>
      </div>
      <div class="col-md-6">
        <h4><?php echo $category_posts->the_title(); ?></h4>
        <span class="date"><?php echo date('M j, Y', $category_posts->get_the_date()); ?></span>
        <p><?php echo implode(' ', array_slice(str_word_count($category_posts->the_content(), 2), 0,120)); ?> [...]</p>
      </div>
    <?php
  }
}

Notice the var_dump($category_posts->the_post()); it comes into here and does this var_dump but the Output is NULL. wtf?

Whats confusing is it has posts, its get into the while statement but the $category_posts->the_post() is null? This makes little to no sense at all.

Update 1

  • Fixed array of arguments suggested by commenter. was order_by, changed to orderby

I have a category, called events. It is assigned to one post. So I created the following loop:

$args = array ('category_name' => 'events', 'posts_per_page' => 4, 'orderby' => 'date', 'order' => 'DESC' );
$category_posts = new WP_Query($args);

if ($category_posts->have_posts()) {
  while ($category_posts->have_posts()) {
    $category_posts->the_post();
    var_dump($category_posts->the_post());

    ?>
      <div class="col-md-6">
        <?php $category_posts->the_post_thumbnail(array('class' => 'events-image')); ?>
      </div>
      <div class="col-md-6">
        <h4><?php echo $category_posts->the_title(); ?></h4>
        <span class="date"><?php echo date('M j, Y', $category_posts->get_the_date()); ?></span>
        <p><?php echo implode(' ', array_slice(str_word_count($category_posts->the_content(), 2), 0,120)); ?> [...]</p>
      </div>
    <?php
  }
}

Notice the var_dump($category_posts->the_post()); it comes into here and does this var_dump but the Output is NULL. wtf?

Whats confusing is it has posts, its get into the while statement but the $category_posts->the_post() is null? This makes little to no sense at all.

Update 1

  • Fixed array of arguments suggested by commenter. was order_by, changed to orderby
Share Improve this question edited Jan 30, 2015 at 7:49 TheWebs asked Jan 30, 2015 at 7:37 TheWebsTheWebs 1,1053 gold badges19 silver badges30 bronze badges 5
  • order_by should be orderby – Pieter Goosen Commented Jan 30, 2015 at 7:46
  • @PieterGoosen Changew as made, still it gives me NULL – TheWebs Commented Jan 30, 2015 at 7:49
  • No its a regular category. And every day category you would see on a post. the post in question even has said category assigned to it, so I should see one post. – TheWebs Commented Jan 30, 2015 at 7:54
  • You then have to check your post status and whether or not a post is password protected and not private. – Pieter Goosen Commented Jan 30, 2015 at 8:06
  • 1 Would be nice if you can share your solution with us :-) – Pieter Goosen Commented Jan 30, 2015 at 8:22
Add a comment  | 

1 Answer 1

Reset to default 2

As per wordpress codex, the_post() "Iterate the post index in The Loop. Retrieves the next post, sets up the post, sets the 'in the loop' property to true." This all occurs within the post object itself.

If you only have 1 post, and run the_post twice, you'll reach the end of the post count. In this case, even doing a var_dump of the_post still runs the function, which is why you're not seeing results below...you've already reached the end of the loop before you start trying to output data.

Also "This function does not return any values."

Meaning your var_dump will always return null, because you're dumping the results of a function which returns nothing.

If you want useful results, var_dump($category_posts) will return the post object, which will contain any posts found and other information. ->the_post is just a method of the post object itself.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745309828a4621938.html

相关推荐

  • wp query - Why is this coming back as null? Thats wrong. There is one post

    I have a category, called events. It is assigned to one post. So I created the following loop:$args = array ('categ

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信