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();
Related
i would like to include an affiliate link as a widget which inserts the title of the respective page as search parameter.
Link
I have already created a shortcode in the functions.php with get_the_title
function post_title_shortcode(){
$variable = get_the_title();
}
add_shortcode('post_title','post_title_shortcode');
and it is called [post_title]
However
didn't work. Does anyone have any other idea how to make this happen?
1-st you have to pass $post object/id in the get_the_title() and 2-nd you have to actually echo this title so you can use it. So it should be something like:
function post_title_shortcode(){
global $post;
$variable = get_the_title($post->ID);
echo $variable;
}
add_shortcode('post_title','post_title_shortcode');
But accessing global $post like this is not recomended, so better to use get_queried_object() and have something like:
function post_title_shortcode(){
global $wp_query;
$term = $wp_query->get_queried_object();
echo $term->post_title;
}
add_shortcode('post_title','post_title_shortcode');
where you'll get the current queried object and whatever you need from it.
I want to display 3 related product on single page in tabs. I have made a custom custom query like this:
$postid = array(1,2,3);
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'post__in' => array_column($postid, 'ID'),
);
$loop = new WP_QUERY($args);
This query works and executes 3 times as well. But in loop I call a another template
wc_get_template_part( 'content', 'single-product-meta-side' );
In content-single-product-meta-side.php the $post variable is reset and returns only the original query post variable.
global $product, $post;
echo $post_id = get_the_ID(); // old/original post id returns
I also tried to setup_postdata( $post ); just after while loop but nothing happens.
Any ideas whats going wrong.
Another query how do I setup the global $product variable.
The function wc_get_template_part uses load_template. This last, requires the template file with some global variables available to ensure that the WordPress environment is available from within the function:
global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
So, either you use the above global variables, or you can locate the file and include it in your scope:
include(locate_template('single-product-meta-side.php'));//Don't forget to set the right path for your file.
I hope this will help you.
I am trying to get post format with the code below
$post_id = $post->ID;
$post_format = get_post_format($post_id);
but it does not provide any response than i tried to check with this code if it provide any result but my code does not go in to this condition.
if(has_post_format('standard')){
print_r($post_id);
}
Please tell me if i am wrong anywhere or there is any other method to get post format.
Thanks
For "$post_id = $post->ID;" to work, you would have to first call the global $post variable global $post; to be able to retrieve the ID outside of the loop.
So, the first code should be:
global $post;
$post_id = $post->ID;
$post_format = get_post_format($post_id);
i want the page id of current page in wordpress . i know get_the_ID() which is used to get the page id when Permalink Settings is Default . But in my case Permalink Settings is Post name and i want the page_id is it possible ? . if yes then how ?
Try This:
<?php
global $post;
echo "pageid: ".$post->ID;
?>
You can try this is you have page name
function get_page_id($page_name){
global $wpdb;
$page_name = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$page_name."'");
return $page_name;
}
get_the_ID() gives you the current ID of a post/page in a loop. Wordpress loads the page or the post in a loop that's why when you run get_the_ID() it gaves you the ID. Now that function has nothing to do with the permalinks. If you're not in any loop (for example you're trying to run that when wordpress is being initialized, you won't get the ID you're looking for because that part is not already set.
get_the_ID() works anywhere no matter what the permalink structure is. The only case i noticed it gives you a result other than the current page or post is when you're already in a loop other that wordpress default's. In that case, get_the_ID() will return the ID of the current post ID in that loop. You can learn more about the loops in the codex.
Now if you're still lost, can you provide a sample of code where you're using that function and you don't get the result you're expecting?
you can use get_page_by_title() if you really want ..
Full parameters are
get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' );
so use like this
$custom_post = get_page_by_title( 'pagename', OBJECT, 'your_custom_post_type' );
$post = get_page_by_title( 'pagename', OBJECT, 'post' );
and just to complete the answer, there is also the similar
get_page_by_path()
$page = get_page_by_path( 'about/us' );
Is it possible in WordPress to get ID of current post in send_headers hook? global $post and $wp_query doesn't work or i did something wrong.
class myClass {
public function __construct() {
add_action('send_headers', array($this, 'myFunction'));
}
public function myFunction() {
// need to get post ID here
}
It looks like WordPress hasn't gotten to the point of initiating $wp_query when the action send_headers is triggered.
As the question asks how to get the post ID this answer assumes that you have the slug in the url instead. If the slug is the last part of the url then this will get the post ID for you.
$post = get_posts ( array (
'name' => pathinfo ( $_SERVER['REQUEST_URI'] )['filename']
) )[0];
echo $post->ID;
This result will need to be tested per use, but should work for most purposes.
You even can try something like this:
global $wp;
$post = get_page_by_path($wp->request);
echo $post->ID;