Wordpress get_posts无法正常工作

时间:2016-11-08 20:59:24

标签: php wordpress

我在自定义页面模板上使用get_posts,以便列出用户是其作者的特定类型(在这种情况下为business)的帖子。 这是我正在尝试的代码:

$args = array(
    'author' => get_current_user_id(),
    'post_type' => 'business',
    'posts_per_page' => -1,
    'post_status' => array(
      'publish',
      'pending',
    )
);
$listings = get_posts( $args );

然而,这会返回用户的所有帖子,无论其类型如何。如果我删除author参数,我会收到所有类型的帖子,而不考虑作者。 get_current_user_id()确实会返回正确的用户ID,但我也尝试在代码中添加ID,并得到相同的结果。

有谁知道为什么会这样?

我正在使用WP 4.6.1

更新: SQL日志记录显示以下内容

如果我使用author参数:

SELECT wp_posts.*
FROM wp_posts
WHERE 1=1
AND wp_posts.post_author IN (1)
AND wp_posts.post_type IN ('post', 'project', 'profile', 'campaign')
AND ((wp_posts.post_status = 'publish'
OR wp_posts.post_status = 'pending'))
ORDER BY wp_posts.post_date DESC

如果我退出author参数:

SELECT wp_posts.*
FROM wp_posts
WHERE 1=1
AND wp_posts.post_type = 'business'
AND ((wp_posts.post_status = 'publish'
OR wp_posts.post_status = 'pending'))
ORDER BY wp_posts.post_date DESC

此外,这是自定义帖子类型声明:

register_post_type('business',
    array(
      'labels' => array(
        'name'          => __( 'Businesses' ),
        'singular_name' => __( 'Business' ),
        'add_new'       =>  __( 'Add New Business' ),
        'add_new_item'  =>  __( 'Add New Business' ),
        'edit_item'     => __( 'Edit Business' )
      ),
      'public'      => true,
      'has_archive' => true,
      'menu_icon'   => 'dashicons-store',
      'supports'    =>  array( 'thumbnail', 'title', 'editor', 'author' )
    )
  );

我在这里不知所措......

1 个答案:

答案 0 :(得分:0)

<?php
global $current_user;


$args = array(
    'author' => $current_user->ID,
    'post_type' => 'business',
    'posts_per_page' => -1,
    'post_status' => 'publish'
);
$listings = get_posts( $args );
?>