I'm using a theme template and when i try to get post ID it returns the ID of the template not the ID of the actual single post.
the template ID is: 215
the post ID is: 1911
the following code will only output 215
function metavalue() {
global $post;
$meta = get_post_meta($post->ID, 'product_url', true);
return $meta;
}
add_shortcode('url_short', 'metavalue');
get_the_ID(); the_id(); $post->ID; will also output 215. i need a way to get the actual single post ID so i can get the custom field value from 'product_url'.
I've contacted support with the theme authors on this topic as well but for the time being i've found a way to work around it.
function metavalue() {
global $wp;
$url = home_url( $wp->request );
$correct_post_id = url_to_postid( $url );
$meta = get_post_meta($correct_post_id, 'product_url', true);
return $meta;
}
add_shortcode('url_short', 'metavalue');
Related
I'm trying to get the value of the custom field 'product_url' this code is going in functions.php and I'm using the shortcode in a single post. The custom field 'product_url' exists on this post and is not empty.
function metavalue() {
GLOBAL $post;
$meta = get_post_meta($post->ID, 'product_url', true);
echo $meta;
}
add_shortcode('url_short', 'metavalue');
nothing is being displayed when I use the shortcode. var_dump($meta); will output
string(0) ""
.
You need to pass $postid to your get_post_meta function.
function metavalue() {
global $post;
$meta = get_post_meta($post->ID, 'product_url', true);
return $meta;
}
add_shortcode('url_short', 'metavalue');
How to debug :
Check product or post have product_url value.
get the ID of product / Post you can get via admin edit post /page.
Pass ID as static value in short code function.
function metavalue() {
global $post;
$meta = get_post_meta(112, 'product_url', true); // 112 static postid
return $meta;
}
add_shortcode('url_short', 'metavalue');
Version 2 : Shortcode with parameter
Combine user attributes with known attributes and fill in defaults when needed.
The pairs should be considered to be all of the attributes which are supported by the caller and given as a list. The returned attributes will only contain the attributes in the $pairs list.
If the $atts list has unsupported attributes, then they will be ignored and removed from the final returned list.
function metavalue($atts) {
$atts = shortcode_atts(
array(
'postid' => 1,
), $atts, 'url_short' );
global $post;
$meta = get_post_meta($atts['postid'], 'product_url', true); // 112 static postid
return $meta;
}
add_shortcode('url_short', 'metavalue');
How to use:
[url_short postid=1911]
I know i can use:
global $wp_query;
$wp_query->is_page = true;
For example to set the global is_page conditional to true.
Can i change the global post title in a similar way so it would effect the the_title() returned value?
To clarify things:
I need for a use of a "virtual page" case, where no post is actually loaded and i don't want to use any existing post title. just inject some custom title to the current globals so i will get it when using the_title on the current page.
To modify the title you can use a build in hook of wordpress:
function suppress_if_blurb( $title, $id = null ) {
if ( in_category(' blurb', $id ) ) {
return '';
}
return $title;
}
add_filter( 'the_title', 'suppress_if_blurb', 10, 2 );
https://codex.wordpress.org/Plugin_API/Filter_Reference/the_title
Finally found it. gladly it's very simple :) but this piece of code will also create all other post vars for a "fake post/page":
$post_id = -99; // negative ID, to avoid clash with a valid post
$post = new stdClass();
$post->ID = $post_id;
$post->post_title = 'Some title or other';
$wp_post = new WP_Post( $post );
wp_cache_add( $post_id, $wp_post, 'posts' );
global $wp, $wp_query;
$wp_query->post = $wp_post;
$wp_query->posts = array( $wp_post );
$wp_query->queried_object = $wp_post;
$wp_query->queried_object_id = $post_id;
$wp_query->is_404=false;
$wp_query->is_page=true;
$GLOBALS['wp_query'] = $wp_query;
$wp->register_globals();
More details here!
I'm trying to show a list of posts inside a var_dump, this is the actual code:
function deleted_cpt_orders() {
global $post_type, $post;
if ( $post_type == 'cpt_orders' ) {
var_dump($post);
}
}
add_action( 'trashed_post', 'deleted_cpt_orders' );
If i delete only 1 post the var_dump is shown, but if i delete 2 posts or more the result is
NULL
global $post is supposed to hold a single post object. If you delete multiple objects, it won't be set. However, trashed_post hook passes post id, therefore, you can do the following
function deleted_cpt_orders($object_id)
{
$post = get_post($object_id);
if ($post->post_type == 'cpt_orders')
{
var_dump($post);
}
}
add_action('trashed_post', 'deleted_cpt_orders');
I have a function in my functions.php file and I need the current post ID.
I have tried getting it like this:
global $wp_query;
$currentID = $wp_query->post->ID;
echo '<pre>';
print_r($currentID);
echo '</pre>';
but doesn't seem to work since it says:
Trying to get property of non-object
EDIT: Entire function in functions.php
add_filter( 'gform_pre_render_1', 'populate_posts' );
add_filter( 'gform_pre_validation_1', 'populate_posts' );
add_filter( 'gform_pre_submission_filter_1', 'populate_posts' );
add_filter( 'gform_admin_pre_render_1', 'populate_posts' );
function populate_posts( $form ) {
global $wp_query;
foreach ( $form['fields'] as &$field ) {
if ( $field->type != 'select' || strpos($field['cssClass'], 'booking-option') === false ) {
continue;
}
$currentID = $wp_query->post->ID;
var_dump($wp_query->post);
$choices[] = array( 'text' => $price, 'value' => $price );
$field->placeholder = '0';
$field->choices = $choices;
}
return $form;
}
Anyone can help me out please
Thanks a lot!
In your function add
global $post;
echo $post->ID;
To have access to the post id you must be within the loop
Otherwise , you must modify your function to accept the post id as a parameter , and hook where is safe to get post id, like so:
add_action('template_redirect', function() {
if (is_single())
your_function(get_queried_object_id());
}
});
function your_function($id){
//Do what you want
}
Some references:
https://wordpress.stackexchange.com/questions/177262/cant-get-post-id-in-functions-php?rq=1
https://wordpress.stackexchange.com/questions/140753/get-current-post-id-in-functions-php
As a note, for the future i think is more appropriate if you post these questions to the http://wordpress.stackexchange.com community
EDIT:
now that you posted the entire code i see that you are using gravity forms (which you didn't mentioned before).
This is a completely different question then.
You must obtain the post_id from the Entry object that gravity forms will pass to your function
https://www.gravityhelp.com/documentation/article/entry-object/
if you want to print the ID then use
the_ID();
if you want to store it then use
$postId = get_the_ID();
Use it in the loop
Hope this helps
Take Care and Happy coding
Getting the current post ID
The ID can be stored as a variable using
<?php $postid = get_the_ID(); ?>
To print
<?php echo $postid; ?>
By function
function get_the_ID() {
$post = get_post();
return ! empty( $post ) ? $post->ID : false;
}
For Reference : click here
How do I add some custom PHP to the "post edit"-page, and get the post_id, when the post is published?
Like this red area:
you can use get_current_screen() function (Link to codex) to check that you're on a post edit page and then get post id with a global $post variable.
Example:
add_action('admin_notices', 'screen_info');
function screen_info() {
$screen = get_current_screen();
if(is_admin() && $screen->id == 'post') {
global $post;
$post_id = get_the_ID();
// your code here
}
}