B
B
BushaevDenis2018-04-23 19:46:06
WordPress
BushaevDenis, 2018-04-23 19:46:06

Why is the post ID 0?

I'm trying to display all products in a category

if ( have_posts() ) {
    while ( have_posts() ) {
        the_post();
        var_dump($post);
    }
}

I get this dump.
object(WP_Post)#13091 (24) {
  ["ID"]=>
  int(0)
  ["post_author"]=>
  string(1) "1"
  ["post_date"]=>
  string(19) "2018-04-17 08:53:16"
  ["post_date_gmt"]=>
  string(19) "2018-04-17 05:53:16"........

How can a post id even be 0?
In the admin panel, this post has a normal ID.
Therefore, then I can’t work with this post in any way and get a Woocommerce product from it.
And the most interesting thing is that I have almost the same cycle on the main page, only I get posts not from functions, but from custom fields, but the output is exactly the same post. But, on the main page it works, but here it doesn't.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
BushaevDenis, 2018-04-23
@BushaevDenis

I did not notice, it turns out that WC gave me not a post, but a category. Moreover, it looks like a post, and the WP_post class. I do not understand how the hierarchy in WC works.

M
Maxim Kudryavtsev, 2018-04-23
@kumaxim

Check if you have wp_reset_postdata() on the main page after the loop

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question