Support » Fixing WordPress » have_posts() wrong result in archive.php

  • Resolved surpriserom


    i was programming my archive.php file for listing my custom post file,
    and i was using the casual loop

    if (have_posts()) :
        while (have_posts()) : the_post();
        my code here

    i have make only 1 post before editing my archive file,
    and the loop was showing nothing
    in fact have_post() was sending false
    and when i had added

      global $wp_query;
      echo '<pre>';
      echo '</pre>';
      echo $wp_query->post_count;

    i’ve seen that $wp_query was sending back for the post count 1 and the function have_post was sending false,
    has i have 1 post the post count was the right one, and when i have added a second post, they where showing correctly, so my question is does the have_post() in archive.php need too have 2 post or is it the custom field wichmake it bug?

Viewing 7 replies - 1 through 7 (of 7 total)
  • esmi


    Forum Moderator

    does the have_post() in archive.php need too have 2 post

    No. Is your post published and publically available?

    yes,it is, and when I unpublished one of the 2 article i have it’s send me also a false, and show me nothing,
    well I will have multiple article too publish so it won’t be a problem, but it surprised me and confused me a bit



    Forum Moderator

    Based on the code snippet you posted above, there’s no reason why that would not work for 1 post if it was the only Loop on the page.

    i think it’s because it is a custom post, because if i try with default post it work

    here is the $arg of my custom post

    $arg = array(
       'labels' => array(
               'name' => __( 'Fiche &eacute;lus', AMF_IDMTXTDMN ),
               'singular_name' => __( 'Fiche &eacute;lu', AMF_IDMTXTDMN ),
               'add_new' => __('Ajouter une fiche d&apos;&eacute;lu', AMF_IDMTXTDMN ),
               'add_new_item' => __('Ajouter une fiche &eacute;lu', AMF_IDMTXTDMN ),
               'edit_item' => __('Editer une fiche &eacute;lu', AMF_IDMTXTDMN ),
               'new_item' => __('Nouvel fiche &eacute;lu', AMF_IDMTXTDMN ),
               'view_item' => __('Voir la fiche &eacute;lu', AMF_IDMTXTDMN ),
               'search_items' =>  __('Rechercher une fiche &eacute;lu', AMF_IDMTXTDMN ),
               'not_found' =>  __('Fiche &eacute;lu non trouvé', AMF_IDMTXTDMN ),
               'not_found_in_trash' => __('Fiche &eacute;lu non trouvé dans la corbeille', AMF_IDMTXTDMN ),
               'parent_item_colon' => ''),
       'rewrite'  => array( 'slug' => 'fiche-elu' ),
       'supports' => array('thumbnail', 'title')



    Forum Moderator

    If this is a custom post type, then you need to add that to your custom query using post_type.

    if found in the archive.php, it was bundled in the theme that i have the function the_post(); who was called before calling the have_posts()
    the theme base is WPUtilities and i just see that he was calling the_post() in the beginning of some file :/
    thanks for help esmi

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘have_posts() wrong result in archive.php’ is closed to new replies.