仅查询登录用户的自定义帖子

时间:2012-12-21 作者:Anagio

我有一个自定义的帖子类型设置,名为website 我认为使用下面的代码只会显示登录用户的帖子,但它会显示所有自定义帖子,甚至不属于登录用户的帖子。有人能告诉我什么是只获取登录用户自定义帖子的查询吗

global $current_user;
      get_currentuserinfo();
$authorID = $current_user->ID;
    $args = array(
              \'post_type\' => \'post\',//or whatever you need
              \'posts_per_page\' => 5,
              \'author\' => $authorID
              );

    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query($args);

1 个回复
SO网友:bueltge

如果用户已登录,请创建一个小插件,将自定义帖子类型添加到默认循环中。

// add custom post type to wp loop
add_filter( \'pre_get_posts\', \'fb_add_to_query\' );

// ads to query
function fb_add_to_query( $query ) {

    if ( ! is_user_logged_in() ) // if user not logged in, return
        return $query;

    if ( is_admin() || is_preview() ) // return, if in backend or preview
        return $query;

    if ( ! isset( $query -> query_vars[\'suppress_filters\'] ) )
        $query -> query_vars[\'suppress_filters\'] = FALSE;

    // conditional tags for restrictions
    if ( is_home() || is_front_page() && ( FALSE == $query -> query_vars[\'suppress_filters\'] ) ) 
        $query->set( \'post_type\', array( \'post\', \'my_post_type\' ) );

    return $query;
}

结束

相关推荐