Receiving messages from guid

I am trying to get a message from it guid.

I tried:

$post = get_post(array('guid' => 'foo'));

      

But this just returns the first post. (this guid is not "foo").

What am I missing?

+3


source to share


2 answers


You cannot pass a GUID to get_post()

.

I would recommend that you create a function that returns the post id from the GUID.



function getIDfromGUID( $guid ){
    global $wpdb;
    return $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE guid=%s", $guid ) );

}

var_dump( get_post( getIDfromGUID('http://localhost/wpdev/?p=10') ) );

      

+11


source


If you use an invalid argument / value, it get_post

will return the data of the first message.

get_post

only accepts $ post_id. http://codex.wordpress.org/Function_Reference/get_post

$post = get_post(7);
$title = $post->post_title;

      



If you want to specify a specific post filtering, you can use get_posts

. http://codex.wordpress.org/Template_Tags/get_posts

Example:

$args = array(
    'posts_per_page'   => 1,
    'category'         => 4,
    'orderby'          => 'post_date',
    'order'            => 'DESC',
    'post_type'        => 'post',
    'post_status'      => 'publish'
 );

get_posts( $args );

      

0


source







All Articles