V
V
vadbobkov2020-06-26 13:53:01
WordPress
vadbobkov, 2020-06-26 13:53:01

How to make a condition in WP that will check if the user has published a post?

Good afternoon. How to implement a condition in WordPress that checks if the user has published a post, if he has published, then a certain text is displayed, something like: This user added a post.
If he did not publish a post, then: This user has not yet added a post.
Thanks for answers.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan Hasanli, 2020-06-27
@azerphoenix

It would be necessary to clarify the details - are you interested in whether the user published a post (i.e. does the user have published posts at all) or did the user publish a new post (i.e. from a certain time interval)?
If the first option, then here are the snippets:
https://wp-kama.ru/function/count_user_posts
https://wordpress.stackexchange.com/questions/2442...

<?php
$user_id = get_current_user_id();  // ID пользователя
$post_type = 'post'; // тип записи. post, page и др.
$posts = count_user_posts( $user_id, $post_type ); //cout user's posts
if( $posts > 0 ){
echo 'У пользователя имеются опубликованные записи';
}
?>

If you are interested in the second option, then this snippet should work:
<?php 
  $args = array(
    'author' => get_current_user_id(), // ID текущего автора
      'date_query' => array(
          array(
              'after'     => 'January 1st, 2015', // задаем временной интервал - С
              'before'    => 'December 31st, 2015', // задаем временной интервал - По
              'inclusive' => true,
          ),
      ),
  );
  $query = new WP_Query( $args );

  $count = $query -> post_count;
  if ($count  != 0) {
    echo 'Данный пользователь добавил пост.';
  } else {
    echo 'Данный пользователь еще не добавил пост.';
  }

?>

https://wp-kama.ru/function/wp_query
https://stackoverflow.com/questions/29275403/get-p...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question