New WP_Query(),Have_Posts()在USER_REGISTER挂接中返回FALSE

时间:2020-05-20 作者:geeksalah

$notifications->have\\u posts()返回false,但我确信数据库中很少有这种自定义类型的帖子

add_action(\'user_register\',\'jq_after_user_register_hook\', 10, 1);
function jq_after_user_register_hook( $user_id ){
    global $wpdb;
    $user_meta = get_userdata($user_id);
    if ( isset( $user_meta->roles ) && is_array( $user_meta->roles ) && in_array(\'subscriber\', $user_meta->roles) ) {

        $args = array(
            \'meta_query\' => array(
                array(
                    \'key\' => \'sending_time\',
                    \'value\' => \'on_registration\',
                    \'compare\' => \'=\',
                )
            ),
            \'post_type\' => \'_jq_notifications\',
            \'posts_per_page\' => -1
        );
        $notifications = new WP_Query($args);
        while ($notifications->have_posts()) : $notifications->the_post();
            $post_id = get_the_ID();
        endwhile;
    }
}
PS:相同的代码块(即新的WP\\U查询)在WP\\U ajax操作中工作
add_action( "wp_ajax__jq_admin_g_nos", "_jq_admin_get_notifications" );

1 个回复
SO网友:geeksalah

我通过添加\'post_status\' => \'draft\'$args 数组,如下所示$args = array( \'meta_query\' => array( array( \'key\' => \'sending_time\', \'value\' => \'on_registration\', \'compare\' => \'=\', ) ), \'post_type\' => \'_jq_notifications\', \'posts_per_page\' => -1, \'post_status\' => \'draft\' );
自定义帖子类型“\\u jq\\u notifications”的默认状态为draft
文档here 表示

post\\u状态:默认值为“publish”,但如果用户登录,则会添加“private”。默认情况下,还包括公共自定义帖子状态。如果查询在管理上下文(管理区域或AJAX调用)中运行,那么也会添加受保护的状态。默认情况下,受保护的状态为“未来”、“草稿”和“待定”。

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post