$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" );
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调用)中运行,那么也会添加受保护的状态。默认情况下,受保护的状态为“未来”、“草稿”和“待定”。