通过guid获取帖子

时间:2014-11-21 03:52:59

标签: wordpress

我正试图通过它的指导得到这个帖子。

我试过了:

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

但这只是返回第一篇文章。 (它的指导不是'foo')。

我错过了什么?

2 个答案:

答案 0 :(得分:12)

您无法在get_post()中传递GUID。

我建议您创建一个从GUID返回帖子ID的函数。

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

答案 1 :(得分:0)

如果你在无效的参数/值中,get_post将返回第一个帖子数据。

get_post只接受$ post_id。 http://codex.wordpress.org/Function_Reference/get_post

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

如果您想要特定的过滤帖子,可以使用get_postshttp://codex.wordpress.org/Template_Tags/get_posts

示例:

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

get_posts( $args );
相关问题