Question

Need help with writing conditional for my wordpress site (cant find similar thread). Dont know if it can be done with wordpress conditional tags.

What i want to accomplish:

If currently logged user published 0 posts show something (for example link number one) else if user published more than 0 posts show something else (link number two).

I managed to write myself simple if statements for displaying menus depending on user role but i cannot find solution for this problem.

Was it helpful?

Solution

WordPress has a count_user_posts() function:

http://codex.wordpress.org/Function_Reference/count_user_posts

OTHER TIPS

Connect to the database using the mysqli_connect PHP function, then taking into account the schema of wordpress databases we have to make the other steps as follows.

The get_current_user_id() wordpress function returns the id of the user who is currently logged in. Then you can use this value like this:

$query = "select count(*) from wp_posts where post_author = ".get_current_user_id();

and finally, you have to run the query as described in the PHP manual.

It was really quite simple (thank you jonahs).

<?php

$user_ID = get_current_user_id();
$user_post_count = count_user_posts( $user_ID );

if ( $user_post_count == 0 ) {
echo 'message one';
} else {
echo 'message two';
}
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top