Make a custom WP_Query and include the ‘post_status‘ argument which is an array of all statuses you want to include in the count. Naturally include the ‘author’ argument with $the_user_id as well. Once instantiated with those arguments, the count will be available in WP_Query::post_count.
Thank you for your answer.
I’ve been trying to make that work, but the value keeps saying NULL.
add_action( 'template_redirect', 'redirect_to_specific_page' );
function redirect_to_specific_page() {
$current_user = wp_get_current_user();
if (empty($the_user_id)) {
$the_user_id = $current_user->ID;
}
$args = array(
'property_type' => 'property',
'author' => $the_user_id,
'post_staus'=> 'any'
);
function custom_count_post_by_author($args){
query_posts( $args );
$count = 0;
if ( have_posts() ) :
while ( have_posts() ) : the_post();
$count++;
endwhile;
endif;
wp_reset_query();
return $count;
}
var_dump(is_page('site1'), $count);
if ( is_page( 'site1' ) && $count == "1" ) {
wp_redirect( 'site2', 301 );
exit;
}
}
Maybe you can tell me what I’m doing wrong?
Because $count in the custom_count_post_by_author() function is local to the function. Outside of the function it has no meaning.
Add this before you var_dump to get an actual count:
$count = custom_count_post_by_author($args);
Aha, it is now working. Thank you very much 🙂