Get_the_id vs. post-> ID vs. the_id / get_post_meta

I think this should be a pretty simple question, but I'm just getting started. Can someone take a look at 3 versions of the same (?) Code below and say what is the difference? They all seem to work fine in the loop I'm working on.

What should I use: $post->ID , $the_ID or get_the_id() ? Do I need to have global $post; ?

 global $post; $content = get_post_meta( $post->ID, 'my_custom_field', true ); echo $content; 

or

 $content = get_post_meta( $the_ID, 'my_custom_field', true ); echo $content; 

or

 $content = get_post_meta( get_the_id(), 'my_custom_field', true ); echo $content; 

Many thanks for your help

+5
source share
1 answer

If you are inside the WordPress loop, then $post->ID will be the same as using get_the_ID()

You do not need to globalize $post , as it is already in the WordPress loop area.

I have never seen code using $the_ID , so I would not use it.

The safest choice is to use get_the_ID()

+4
source

Source: https://habr.com/ru/post/1207056/


All Articles