I think it must be pretty basic question but I am only starting. Can someone have a look at the 3 versions of the same (?) code below and say what the difference is? All of them seem to work fine in the loop I am working on.
Which should be used: $post->ID, $the_ID or get_the_id()?
Is it necessary 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
If you're inside a WordPress loop, then $post->ID it's the same as using get_the_ID()
You shouldn't need to globalize $post since it's already in the scope of a WordPress loop.
I've never seen code using $the_ID, so I would avoid using that.
The safest choice would be to use get_the_ID()
Related
I'm trying to make my first WP plugin, but stuck on simple function - can't get ID of post inside it.
I have tried:
$post_id = get_the_ID();
After that I thought, that my plugin works outside loop and try this:
global $post;
$post_id = $post->ID;
Few hours later I have tried to create function, that get post id after INIT:
function postidfinder () {
global $post;
$post_id = $post->ID;
return $post_id
}
add_action( 'init', 'postfinder' );
Also tried actions: wp_loaded, loop_query.
Please, help get post ID to the plugin. Thanks!
As far I know, to use post->ID outside of loop, wp_query should be called first.
global $wp_query;
$postid = $wp_query->post->ID;
Optionally, get_post_id() can work for you, check codex for more.
From the Global $post object :
Global object $post contains a lot of data of the current post. It is very easy to get ID from the object:
global $post;
echo $post->ID;
Using get_the_id() and the_id() functions:
The difference between this two functions is that get_the_id() returns ID of the current post, and the_id() prints it.
echo get_the_id();
the_id();
I have a custom page template with certain custom fields. I want to display these custom fields outside the loop, but within the same page.
This one works:
<?php echo get_post_meta( '244', 'custom_field_name', true ) ?>
But I want to to work dynamically, without me entering the actual ID of the page.
How can I call the page ID in the echo?
Try this :
<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'Your-Custom-Field', true);
wp_reset_query();
?>
If this call is in the loop, replace the id with function get_the_ID, this function retrieves the ID of the current item in the WordPress Loop.
<?php echo get_post_meta( get_the_ID(), 'custom_field_name', true ) ?>
See: https://developer.wordpress.org/reference/functions/get_the_ID/
If this call is in the single page, replace the id with object item $post->ID.
$post = get_post();
<?php echo get_post_meta( $post->ID, 'custom_field_name', true ) ?>
See: https://codex.wordpress.org/Class_Reference/WP_Post
Also, you can get the access to the post via global variable $post.
$post = get_post();
<?php echo get_post_meta( $post->ID, 'custom_field_name', true ) ?>
See: https://codex.wordpress.org/Global_Variables
so I looked through a number of similar topics here and couldn't for the life of me replicate the techniques to this.
I'm working Wordpress and instead of adding the following line:
<a class="button alt live_demo" href="<?php echo get_post_meta( $post->ID, '_live_demo', true ); ?>">Live Demo</a>
..directly into Wordpress template files, I'm trying to hook in via functions.php. So first I added the function to the hook where I want it to go:
add_action('woocommerce_before_add_to_cart_button', 'add_live_demo_link');
Then created the function:
function add_live_demo_link() {
echo '<a class="button alt live_demo" href="<?php echo get_post_meta( $post->ID, '_live_demo', true ); ?>">Live Demo</a>';}
Obviously that code won't work, can anyone help converting that to a working function?
Couple of issues with your code. 1. you are not globally declaring $post so you'll never retrieve any meta. 2. You are echoing an echo, which will probably also not work.
I didn't test this so I may have made a typo, but this is on the right track:
add_action('woocommerce_before_add_to_cart_button', 'add_live_demo_link');
function add_live_demo_link() {
global $post;
$meta = get_post_meta( $post->ID, '_live_demo', true );;
printf( '<a class="button alt live_demo" href="%s">%s</a>', esc_url( $meta ), __( 'Live Demo', 'my-plugin' ) );
}
I'm sending array meta box data using update_post_meta like below. However I cannot seem to output the post meta array value into an empty input. The meta is being stored correctly.
if( get_post_meta( $post->ID, 'date-meta', true ) ) {
$date_info = get_post_meta( $post->ID, 'date-meta', true );
}
My input field looks like this:
<input type="date" class="widefat" name="vp-date" id="vp-date" value="<?php echo $date_info['vp-date']; ?>" />
I also get a notice which traces back to the if get_post_meta function above. It says:
Trying to get property of non-object in
Any help would be great.
Thanks
That error message means that the $post is $post->ID is not an object. I don't know what script you are in but try putting
global $post;
above the if.
Making it
global $post;
if( get_post_meta( $post->ID, 'date-meta', true ) ) {
$date_info = get_post_meta( $post->ID, 'date-meta', true );
}
I have a PHP statement
<?php echo get_post_meta( $post->ID, 'jetpack-post-views', true ); ?>
This i used by a wordpress plugin called "Jetpack post views"
It asked me to place the provided code anywhere on the page to see the post views.
I want it on "genesis_entry_header" with the other post_info
I don't know how to do that, and I look everywhere for a solution before I post this.
Thank you
Try with this,
function wp_87978_jpv(){
global $post;
echo get_post_meta( $post->ID, 'jetpack-post-views', true );
}
add_action('genesis_entry_header', 'wp_87978_jpv');