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;
Related
I want to modify a custom field in my WordPress edit post page and I will need the post ID to do this. I have a function in functions.php and it works fine when I manually enter the post ID. The issue comes up when I try getting the post ID from the URL but the $_GET['post'] is proving to be useless. var_dump($_GET['post']) returns NULL, $global $post; var_dump($post->ID) returns NULL. The URL is the normal post edit link
URL: http://mywebsite.com/wp-admin/post.php?post=435&action=edit
I have been able to get the post ID via the admin_head hook but can't get it to work in my other function suing set_query_var(). Have a look below at my modified code:
add_action( 'admin_head', 'get_post_ID' );
function get_post_ID() {
global $post;
$thePostID = 0;
$pagenow = isset($GLOBALS['pagenow']) ? $GLOBALS['pagenow'] : null;
if ( $pagenow == 'post.php' ) {
$thePostID = $post->ID;
set_query_var('my_post_id', $thePostID);
}
return $post->ID;
}
function get_admin_post_ID() {
var_dump(get_query_var('my_post_id'));
}
add_action( 'template_redirect', 'get_admin_post_ID' );
What could be the issue here? Is there something I am missing? Kindly assist.
So, what $_GET return instead?
Are you using some plugin that edit posts, and that do not use/pass this var?
As said before, the browser address url, return something like this or not?
https://subdomain.w3host.com/wordpress/wp-admin/post.php?post=100&action=edit
PS: var_dump that return NULL on $_GET['post'], simply mean that there is so, a php
Notice: Undefined index:
the post var do not exist
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 need to create dynamic urls all loading same page (please note loading not redirecting) plugins that I could find only do redirects. Basically what I need is:
/somepage/something
/somepage/anotherthig
/somepage/thething/morethings
all loading existing page
/somepage
but the original url must be kept (not a redirect). Any advice on how t do it ( a plugin that does this works as well if you know of one) is greatly appreciayed.
Shouldn't be that hard and can be achieve by modifying $wp_query and $post global variable,
try this code
// modify variable by hooking it on 'wp' action
add_action( 'wp', function() {
global $wp, $wp_query, $post; //define global variable
//include $wp variable so you can check the url request
// list the url you want to use
$dynamic_url = [
'somepage/something',
'somepage/anotherthig',
'somepage/thething/morethings'
];
// check if page request is found from the array above
if ( in_array( $wp->request, $dynamic_url ) ) {
// build query argument
$args=[
'post_type' => 'page', //assuming its a page
'p' => 26 // page ID of the page you want to display on those dynamic URLS
];
// run the query and assign it to $wp_query global variable
$wp_query = new WP_Query( $args );
// modify is_single wp_query param and tell it its not a post
$wp_query->is_single = '';
// modify is_page wp_query param and tell it its a page
$wp_query->is_page = 1;
//assign (1st) found post to global post variable
$post = $wp_query->posts[0];
//modify header as 202 status (unless you want these pages to stay as 404), by defualt its a 404
status_header( 202 );
//done
}
});
You can use this plugin to create dynamic url's. this plugin is free.
https://wordpress.org/plugins/sdk-wp-dynamic-url/
if you are looking something advance then this plugin will surely do for you but its paid:
https://wordpress.org/plugins/if-so/
Hope that Helps
Happy Coding
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 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' );