new WP_Query(), have_posts() return false in user_register hook

Question

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" );

0
, , geeksalah 4 years 2020-05-20T03:10:48-05:00 0 Answers 88 views 0

Leave an answer

Browse
Browse