get_post_format() function not working Wordpress - php

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);

Related

Current WordPress Page Title as Search Parameter into A Tag

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.

Can't get post_ID inside wordpress plugin

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();

how to get page id in wordpress if Permalink is Post name

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' );

Getting post id on send_headers action in WordPress

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;

Preserving postdata with embedded wp_query queries?

I'm designing a website in Wordpress. This site is one of those parallax sites where all the pages are printed on the homepage and the menu scrolls to the anchors.
That being said I am using a wp_query to pull out all the pages that are in the main menu. Furthermore I have a shortcode that I use in the content that also requires the use of wp_query.
The problem I have is that the shortcode (the embedded wp_query) is screwing up the postdata. I know when using wp_query you'd usually want to use wp_reset_postdata but in this particular situation it doesn't work because this function call will restore the postdata of the homepage and not of the currently running wp_query (sorry if I'm being unclear).
Is there a way to take a snapshot of the postdata to then restore after my shortcode? I'm looking for something along the lines of:
function my_shortcode() {
save_postdata(); //saves the current postdata
$query = new WP_Query();
while( $query->have_posts() ) {
$query->the_post();
echo get_the_title();
}
my_wp_reset_postdata(); //restores the postdata to where it was before the loop
}
By looking in the source for wp_reset_query(), you will see that what it does is that it simply restores the $wp_query global variable from another global variable($wp_the_query - this is set-up together with the initial set-up for $wp_query, so it holds the original query).
What you can do is you can simply assign $wp_query to a different global variable and then later restore it. Here's an example:
function _save_query( $var = '_wp_query' ) {
$GLOBALS[ $var ] = $GLOBALS['wp_query'];
}
function _wp_reset_query( $var = '_wp_query' ) {
$GLOBALS['wp_query'] = $GLOBALS[ $var ];
wp_reset_postdata();
}
So simply call _save_query() before overwriting the query(you can pass a custom variable name - this way you can store multiple WP_Query objects :) ).
Once you want to restore the query data, call _wp_reset_query() - again you can pass a string as a variable name in order to restore this exact query object.
This is how I managed to get it working, credit goes to Nikola's question since I worked off of his idea.
function _save_query( $var = '_wp_query' ) {
global $post;
$GLOBALS[ $var ] = $post;
}
function _wp_reset_query( $var = '_wp_query' ) {
global $post;
$post = $GLOBALS[ $var ];
setup_postdata( $post );
}
I looked at the documentation of how the loop works found here. I decided to use the same kind of setup as in Nikola's answer since it met my criteria but I used the implementation of the_post to restore the postdata. This is probably not very efficient since it's using the setup_postdata function (which I assume is overkill) but it has definitely solved my problem.
So now when I embed a wp_query I can just do the following:
_save_query();
$products = new WP_Query( $args );
if( $products->have_posts() ) {
$ob .= '<ul class="group-posts">';
while ( $products->have_posts() ) {
$products->the_post();
$ob .= '<li>'.get_the_title().'</li>';
}
_wp_reset_query();
$ob .= '</ul>';
}
Side question/note: What's the etiquette for marking an answer as the correct answer? I'd feel bad accepting my answer as the correct one when Nikola helped me reach it?
my_wp_reset_postdatadoes not exist. You have to use wp_reset_postdata(). But in a situation where you have to chain multiple wp_queries and come back to the older ones, you can store your first query in a variable, set the new WP_query, then reset it and come back to the old one.
$wp_query stores the current loop. So you can go something like :
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query($args);
...
// then later
$wp_query = $temp;
// And back on tracks !

Categories