$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>functions - Checking is user author of number of posts?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

functions - Checking is user author of number of posts?

matteradmin10PV0评论

I have this function...

$user = wp_get_current_user();
if (( in_category('Locked') ) && in_array( 'subscriber', (array) $user->roles ) ) {
    /* Is subscriber, is in category Locked, has amount of posts */
    echo do_shortcode('[shortcode_name]');

} else if (( in_category('Locked') ) && in_array( 'subscriber', (array) $user->roles ) ) {
    /* Is subscriber, is in category Locked, has NO amount of posts */
    echo '<div id="locked">
You are subscriber without number of posts!
</div>';

} else if ( in_category('Locked') ) {
    /* Is NOT subscriber, is in category Locked, has NO amount of posts */
    echo '<div id="locked">
Login or register pal!
</div>';

} else { 
    /* Is NOT subscriber, is NOT in category Locked, has NO amount of posts */
    echo do_shortcode('[shortcode_name]'); 
}

I need to apply "has amount of posts" or "check if user is author of numebr of posts" on first part of code...

if (( in_category('Locked') ) && in_array( 'subscriber', (array) $user->roles ) ) && ?????

If this way can't work, I would have one more possible solution, it is to auto move user from subscriber to contributor once subscriber posted number of posts, but this first solution would be better.

I have this function...

$user = wp_get_current_user();
if (( in_category('Locked') ) && in_array( 'subscriber', (array) $user->roles ) ) {
    /* Is subscriber, is in category Locked, has amount of posts */
    echo do_shortcode('[shortcode_name]');

} else if (( in_category('Locked') ) && in_array( 'subscriber', (array) $user->roles ) ) {
    /* Is subscriber, is in category Locked, has NO amount of posts */
    echo '<div id="locked">
You are subscriber without number of posts!
</div>';

} else if ( in_category('Locked') ) {
    /* Is NOT subscriber, is in category Locked, has NO amount of posts */
    echo '<div id="locked">
Login or register pal!
</div>';

} else { 
    /* Is NOT subscriber, is NOT in category Locked, has NO amount of posts */
    echo do_shortcode('[shortcode_name]'); 
}

I need to apply "has amount of posts" or "check if user is author of numebr of posts" on first part of code...

if (( in_category('Locked') ) && in_array( 'subscriber', (array) $user->roles ) ) && ?????

If this way can't work, I would have one more possible solution, it is to auto move user from subscriber to contributor once subscriber posted number of posts, but this first solution would be better.

Share Improve this question edited Nov 28, 2018 at 22:22 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Nov 28, 2018 at 22:14 MLLMLL 555 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

I guess count_user_posts is what you're looking for ;)

This is how you use it:

$user_post_count = count_user_posts( $userid , $post_type );

And it returns the number of published posts the user has written in this post type.

PS. And if you want some more advanced count, get_posts_by_author_sql can come quite handy.

Guy above answered correctly, but for anyone needing this further, I will add full code as response too...

$user = wp_get_current_user();
$user_ID = get_current_user_id();
$user_post_count = count_user_posts( $user_ID );
$my_post_meta = get_post_meta($post->ID, 'shortcode_name', true);


if (( in_category('Locked') ) && in_array( 'subscriber', (array) $user->roles ) && $user_post_count == 5 ) {
    /* Is subscriber, is in category Locked, has amount of posts */
    echo do_shortcode('[shortcode_name]');

} else if (( in_category('Locked') ) && in_array( 'subscriber', (array) $user->roles ) ) {
    /* Is subscriber, is in category Locked, has NO amount of posts */
    echo '<div id="locked">
You are subscriber without number of posts!
</div>';
} else if (( in_category('Locked') ) && in_array( 'administrator', (array) $user->roles ) ) {
    /* Is subscriber, is in category Locked, has power */
echo do_shortcode('[shortcode_name]'); 

} else if ( in_category('Locked') ) {
    /* Is NOT subscriber, is in category Locked, has NO amount of posts */
    echo '<div id="locked">
Login or register pal!
</div>';

} else if ( ! empty ( $my_post_meta ) ) { 
    /* Post meta exist */
echo do_shortcode('[shortcode_name]'); 


} else { 

    /* Is NOT subscriber, is NOT in category Locked, has NO amount of posts */
    /* Post meta NOT exist */
    echo do_shortcode('[shortcode_name_1]'); 
}
Post a comment

comment list (0)

  1. No comments so far