wp query - new WP_Query(), have_posts() return false in user_register hook

The $notifications->have_posts() return false, though I'm sure there are few posts of this custom type, in the d

The $notifications->have_posts() return false, though I'm sure there are few posts of this custom type, in the database.

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:The same block of code (i.e. new WP_Query) works in wp_ajax action
add_action( "wp_ajax__jq_admin_g_nos", "_jq_admin_get_notifications" );

The $notifications->have_posts() return false, though I'm sure there are few posts of this custom type, in the database.

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:The same block of code (i.e. new WP_Query) works in wp_ajax action
add_action( "wp_ajax__jq_admin_g_nos", "_jq_admin_get_notifications" );

Share Improve this question asked May 20, 2020 at 7:44 geeksalahgeeksalah 112 bronze badges 1
  • I have experience before that custom post types and taxonomies are actually not yet ready in certain hook. You may find out in this direction. Example, you you try 'init' hook to do the same thing to see if there is any results. – 西門 正 Code Guy - JingCodeGuy Commented May 21, 2020 at 3:22
Add a comment  | 

1 Answer 1

Reset to default 0

I resolved this by adding 'post_status' => 'draft' to the $args array, as below
$args = array( 'meta_query' => array( array( 'key' => 'sending_time', 'value' => 'on_registration', 'compare' => '=', ) ), 'post_type' => '_jq_notifications', 'posts_per_page' => -1, 'post_status' => 'draft' );
The custom post type "_jq_notifications" default status was draft.
The documentation here says

post_status: Default value is ‘publish‘, but if the user is logged in, ‘private‘ is added. Public custom post statuses are also included by default. And if the query is run in an admin context (administration area or AJAX call), protected statuses are added too. By default protected statuses are ‘future‘, ‘draft‘ and ‘pending‘.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742407382a4438125.html

相关推荐

  • wp query - new WP_Query(), have_posts() return false in user_register hook

    The $notifications->have_posts() return false, though I'm sure there are few posts of this custom type, in the d

    11小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信