WordPress Query on Author.php not working

The following query is not working on the WordPress author.php template for my theme. Its included in the footer which is the same footer across all pages and the query works fine on every other page except for author.php

  <?php if(have_posts()):?>
  <?php query_posts( array( 'post_type' => 'connect' ) ); while (have_posts()) : the_post(); ?>
  <div class="title"><?php the_title();?></div>
  <div class="logos">  
  <?php the_content();?>
  </div>
  <?php endwhile;?>
  <?php wp_reset_query(); ?>
  <?php endif;?>

I have spent more than an hour trying to figure out what is going on and why this isn’t working, but I now feel like I am banging my ahead against concrete. Why isn’t it working?!

Related posts

Leave a Reply

2 comments

  1. In order for me to explain why it’s only working on some of the pages, you need to understand what query_posts() actually does.

    query_posts() modifies the default WordPress Loop. No matter what page you’re on, there is ALWAYS a default Loop initialized by the Core. Unless your intention is to modify that loop, it is imperitive that you stop using query_posts() altogether.

    There are MANY reasons why query_posts() is misused so often, and they have been detailed out in many forums as well as in the WordPress Codex itself. But that’s getting into an area that is irrelevant to your question.

    First off, let’s look at what your code is doing:

    <?php if(have_posts()):?> //If the default loop finds posts....
    <?php query_posts( array( 'post_type' => 'connect' ) );?> //Modify the loop to fit these new parameters
    

    Basically, your new query will only run if the Default Loop is able to return a set of results. This works on other pages, because the Default Loop will generally work in most scenarios.

    It’s not working on your Author.php template because, for whatever reason, it’s not able to return a set of results to then run your modified query.

    So how do you fix it?

    You need to change your structure, and how your queries are called. I don’t know how deep into the project you are, and if this is something that is on a pretty tight deadline with a client, but my advice would be to scrap any and all query_posts() calls in favor of WP Query

    Does it look a little more complicated? Sure. But making this your bread and butter of any current and future WordPress themes will end up saving you a LOT of time and trouble.

    The bad way:

    <?php
    query_posts( array( 'post_type' => 'connect' ) );
    if(have_posts()): while (have_posts()) : the_post();
    ?>
    <div class="title"><?php the_title();?></div>
    <div class="logos">  
    <?php the_content();?>
    </div>
    <?php
    endwhile;
    wp_reset_query();
    endif;
    ?>
    

    The proper way:

    <?php
    $q = new WP_Query( array( 'post_type' => 'connect' ) );
    if($q->have_posts()) : while($q->have_posts()) : $q->the_post;
    ?>
    <div class="title"><?php the_title();?></div>
    <div class="logos">  
    <?php the_content();?>
    </div>
    <?php
    endwhile;
    wp_reset_postdata();
    endif;
    ?>
    

    Hope this helps, and good luck.

    UPDATE:

    WP_Query does allow you to query posts by author, and your assumption that default values provided within a new WP_Query object will typically reflect the default query on a given page seems to make sense, and can potentially explain the behavior you’re seeing.

    Since the WP_Query documentation doesn’t really provide a way to explicitly search for author type ‘any’, we may have to get our hands a little dirty on this one:

    $user_ids = get_users(array('who'=>'author', 'fields'=>'ID'));
    $q = new WP_Query( array( 'post_type' => 'connect', 'author'=>implode(',', $user_ids) ) );
    

    Let me know if that helps.

  2. try to use wp query instead

    $the_query = new WP_Query();
    $the_query->query(array( 'post_type' => 'connect' ));
    if ($the_query->have_posts()) : 
    while($the_query->have_posts()) : $the_query->the_post();
    
    endwhile; 
    endif; 
    wp_reset_postdata();
    

    Or you can also try to use get_posts if wp_query doesnt work. Im pretty sure this will work in author.php

    global $post;
    $args = array( 'post_type' => 'connect' );
    $posts = get_posts( $args );
    foreach( $posts as $post ): setup_postdata($post); 
       //you can call the_title the_content and any other method that runs under query_posts and WP_Query
    endforeach;