Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting posts by guid

Tags:

wordpress

I'm trying to get the post by it's guid.

I tried:

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

But that just returns the first post. (it's guid is not 'foo').

What am I missing?

like image 658
pguardiario Avatar asked Nov 21 '14 03:11

pguardiario


1 Answers

You can't pass GUID in get_post().

I'd recommend you creating a function that returns a post ID from a 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') ) );
like image 141
RRikesh Avatar answered Nov 15 '22 21:11

RRikesh