Get posts on template redirect

I'm trying to validate some $_GET parameters before letting the user view a particular page. In the actual template file, this code works:

$user = get_user_by('ID', $_GET['sscp']);

$args = [
    'post_type' = 'appointment',
    'author' = $user-ID,
    'meta_query' = [
        [
            'key' = 'clinic',
            'value' = serialize( [ wp_get_current_user()-dr_clinic_assigned ] )
        ]
    ]
];
$posts = get_posts($args);
echo pre;
print_r( $posts );
echo /pre;

But when I do add_action( 'template_redirect', 'my_redirect_function' ); it doesn't show any results. I want to do this in template_redirect because I want to make use of wp_safe_redirect functionality. As per my understanding, wp_safe_redirect only works when no headers are sent yet.

Topic template-redirect get-posts wp-query Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.