Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

221
Visualizações
Wordpress how to check if a query has posts

This code allows me to display to the connected user the list of his written articles. However, I would like to choose the message that appears when no articles have been written (when the list is empty).

I know that I need an IF statement but I don't really know where to put it and with which data.

I would like the user who has no articles to see "no articles" written.

Thanks in advance,

Here is my code:

add_action( 'woocommerce_account_dashboard' , 'recent_posts', 3 );
function recent_posts() {
    if ( is_user_logged_in() ):

    global $current_user;
    wp_get_current_user();
    $author_query = array('posts_per_page' => '-1','author' => $current_user->ID);
    $author_posts = new WP_Query($author_query);
    ?><div id="recentposts">
            
    <ul class="liststylenone">
        
    <?php
    while($author_posts->have_posts()) : $author_posts->the_post();
    ?>
            <li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>   
    <?php           
    endwhile;
    ?></ul><?php
else :
    echo "not logged in";
endif;

?>
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

When you use wp_query, it has a property called found_posts. Right before your ul tag we're going to check whether there is any post in your query. If there is no post found, then we're going to echo a p tag with a message. Like so:

add_action('woocommerce_account_dashboard', 'recent_posts', 3);

function recent_posts()
{
  if (is_user_logged_in()) :

    global $current_user;

    $author_query = array('posts_per_page' => '-1', 'author' => $current_user->ID);

    $author_posts = new WP_Query($author_query);

?>
    <div id="recentposts">
      <?php
      if ($author_posts->found_posts) {
      ?>
        <ul class="liststylenone">
          <?php
          while ($author_posts->have_posts()) : $author_posts->the_post();
          ?>
            <li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
          <?php
          endwhile;
          ?>
        </ul>
  <?php
      } else {
        echo '<p class="author-no-post-yet">No Articles</p>';
      }
    else :
      echo "not logged in";
    endif;
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda