Query showing for all authors

This comes from my other thread as it is not working as I thought. It is querying the custom post type on the author pages but it is showing all of the Custom Posts on all of the author's pages, not just the ones written by that author.

Here is the other thread:

https://premium.wpmudev.org/forums/topic/author-custom-post-types

Here is my current code. I have two author pages set up and one custom post type. The one post shows on both author pages.

<?php	global $wp_query;
query_posts( array(
    'post_type' => 'ShopResources' ,
    'author=' . $authorid,
    'showposts' => 20)
); ?>
<h3 align="center">Resources For Sale</h3>
<ul>
 <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
  • Philip John

    Hiya Eric,

    This code doesn't look right to me;

    query_posts( array(
        'post_type' => 'ShopResources' ,
        'author=' . $authorid,
        'showposts' => 20)
    );

    It appears to be a mismatch of different parameter styles. You should change it to either one of this

    query_posts( array(
        'post_type' => 'ShopResources' ,
        'author' => $authorid,
        'showposts' => 20)
    );

    or

    query_posts(
        'post_type=ShopResources'.
        '&author=' . $authorid.
        '&showposts=20')
    );

    I'd recommend the former.

    Phil

  • Eric G.

    Ok here is what I have so far. I did some research on having multiple loops and queries and I am just messing things up more than they were. So I brought it back to the basics, thought I would throw it at you and see if you can help.

    I have two example set up. Each author has one regular post and one Custom Post (ShopResources). Currently it is filtering the author but not showing the Custom Post on the right side.

    The links: http://youthministryresources.com/author/kdklipfel/ & http://youthministryresources.com/author/ericgallagher/

    The code:

    <?php get_header(); ?>
    
    <div id="content" class="narrowcolumn">
    
    <!-- This sets the $curauth variable -->
    <br/>
    
        <?php
        $curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
        ?>
    
    <div id="authormain">
    <div id="authorleft">
    <?php echo get_avatar( $curauth->user_email, '125' ); ?>
    </div>
    <div id="authorcenter">
    <h2><strong>Youth Worker Profile</strong><br/></h2>
    
    <?php if ($curauth->first_name == '') { ?>
    <h3><?php echo $curauth->display_name; ?> - Profile Yours?  <a href="http://youthministryresources.com/wp-admin/profile.php" target="_blank">Update it here</a></h3>
    <?php } else { ?>
    <h3><?php echo $curauth->first_name; ?> <?php echo $curauth->last_name; ?></h3>
    <?php } ?>
    
    <?php if ($curauth->user_description == '') { ?>
    <?php } else { ?>
    <p><strong>About: </strong>
    <?php echo $curauth->user_description; ?></p>
    <?php } ?>
    </div>
    
    <div id="authorright">
    <div id="voteboxauthortotal"><div align="center">Total Votes Received<br/><?php wdpv_vote_result(false); ?></div></div>
    <?php if ($curauth->user_url == '') { ?>
    No Website
    <?php } else { ?>
    
    <div id="authorwebbox"><div align="center"><a href="<?php echo $curauth->user_url; ?>">Visit <?php echo $curauth->first_name; ?>'s Website</a></div></div>
    <?php } ?>
    </div>
    </div>
    
    <br/>
    
    <div id="clear"></div>
    
    <?php if ($curauth->first_name == '') { ?>
    <h2 align="center"><?php echo $curauth->display_name; ?>'s Contributions</h2>
    <?php } else { ?>
    <h2 align="center"><?php echo $curauth->first_name; ?>'s Contributions</h2>
    <?php } ?>
    
    <div id="postsbox">
    
    <!-- The Loop -->
    
    <h3 align="center">Free Youth Ministry Resources</h3>
    <!-- Filter and Show Author's Free Resources -->
    <ul>
        <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
            <li>
                <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>">
                <?php the_title(); ?></a>
            </li>
        <?php endwhile; else: ?>
            <p align="center"><?php _e('This youth worker has not posted any free resources yet...'); ?></p>
        <?php endif; ?>
    	</ul>
    	</div>
    
    	<div id="postsbox">
    <!-- Filter and Show Author's Shop Products -->
    
    <h3 align="center">Resources For Sale</h3>
    <ul>
     <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
            <li>
                <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>">
                <?php the_title(); ?></a>
            </li>
    
        <?php endwhile; else: ?>
    	<h3><?php echo $curauth->first_name; ?>'s resources for sale in our shop:</h3>
            <p><?php _e('This youth worker has no resources in our shop yet...'); ?></p>
    
        <?php endif; ?>
    	 </ul>
    	</div>
    <!-- End Loop -->
    
    </div>
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>
  • Philip John

    Okay you have several loops there that start with;
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    That's fine, but what you're not doing is specifying what the loop should include. Do that like this;

    <?php query_posts( array(
        'post_type' => 'ShopResources' ,
        'author' => $authorid,
        'showposts' => 20)
    ); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    ...changing the arguments of query_posts as required. You either use query_posts OR WP_Query (as in the example you posted) but both will work.

    Then, you are ending the loop with;
    <?php endif; ?>

    That's also fine but you're not refreshing the Query which means subsequent loops will use the last set of arguments. So you need this;
    <?php wp_reset_query(); endif; ?>

    You also need to remove this:

    // Reset Post Data
    wp_reset_postdata();

    That should sort it.... *crosses fingers*

    Phil

  • Eric G.

    Back again :wink:

    I believe I made all of the changes as you suggested. Now have two problems that have been consistent. 1. Author pages are still showing ALL posts so the filter for the author is not working.
    2. The 2nd query is supposed to have two posts by the two different authors but only picks up the one.

    Here's what I have now.

    <?php get_header(); ?>
    
    <div id="content" class="narrowcolumn">
    
    <!-- This sets the $curauth variable -->
    <br/>
    
    	<?php
    $curauth = (get_query_var('author_name')) ? get_user_by('slug', get_query_var('author_name')) : get_userdata(get_query_var('author'));
    ?>
    
    <div id="authormain">
    <div id="authorleft">
    <?php echo get_avatar( $curauth->user_email, '125' ); ?>
    </div>
    <div id="authorcenter">
    <h2><strong>Youth Worker Profile</strong><br/></h2>
    
    <?php if ($curauth->first_name == '') { ?>
    <h3><?php echo $curauth->display_name; ?> - Profile Yours?  <a href="http://youthministryresources.com/wp-admin/profile.php" target="_blank">Update it here</a></h3>
    <?php } else { ?>
    <h3><?php echo $curauth->first_name; ?> <?php echo $curauth->last_name; ?></h3>
    <?php } ?>
    
    <?php if ($curauth->user_description == '') { ?>
    <?php } else { ?>
    <p><strong>About: </strong>
    <?php echo $curauth->user_description; ?></p>
    <?php } ?>
    </div>
    
    <div id="authorright">
    <div id="voteboxauthortotal"><div align="center">Total Votes Received<br/><?php wdpv_vote_result(false); ?></div></div>
    <?php if ($curauth->user_url == '') { ?>
    No Website
    <?php } else { ?>
    
    <div id="authorwebbox"><div align="center"><a href="<?php echo $curauth->user_url; ?>">Visit <?php echo $curauth->first_name; ?>'s Website</a></div></div>
    <?php } ?>
    </div>
    </div>
    
    <br/>
    
    <div id="clear"></div>
    
    <?php if ($curauth->first_name == '') { ?>
    <h2 align="center"><?php echo $curauth->display_name; ?>'s Contributions</h2>
    <?php } else { ?>
    <h2 align="center"><?php echo $curauth->first_name; ?>'s Contributions</h2>
    <?php } ?>
    
    <div id="postsbox">
    
    <!-- The Loop -->
    
    <h3 align="center">Free Youth Ministry Resources</h3>
    <!-- Filter and Show Author's Free Resources -->
    <ul>
    
    <?php query_posts( array(
        'post_type' => 'post',
        'author' => $authorid,
        'showposts' => 20)
    ); ?>
    
        <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
            <li>
                <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>">
                <?php the_title(); ?></a>
            </li>
        <?php endwhile; else: ?>
            <p align="center"><?php _e('This youth worker has not posted any free resources yet...'); ?></p>
        <?php wp_reset_query(); endif; ?>
    	</ul>
    	</div>
    
    	<div id="postsbox">
    <!-- Filter and Show Author's Shop Products -->
    
    <h3 align="center">Resources For Sale</h3>
    
    <?php query_posts( array(
        'post_type' => 'ShopResources' ,
        'author' => $authorid,
        'showposts' => 20)
    ); ?>
    
    <ul>
     <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
            <li>
                <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>">
                <?php the_title(); ?></a>
            </li>
    
        <?php endwhile; else: ?>
    	<h3><?php echo $curauth->first_name; ?>'s resources for sale in our shop:</h3>
            <p><?php _e('This youth worker has no resources in our shop yet...'); ?></p>
    
        <?php wp_reset_query(); endif; ?>
    	 </ul>
    	</div>
    <!-- End Loop -->
    
    </div>
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    Thanks again for helping with this. I usually wouldn't be so persistent, but if I learn how to do this it will help with several other areas of my site.

  • Jonathan

    @Eric G.
    Firstly, are you using the author.php template page?
    and you are trying to list authors posts (custom post types) on his /author/username page?

    If so this code should work

    First Part:

    <?php $args = array(
    	'post_type'=> 'post',
    	'showposts' => 20
    );
    query_posts( $args ); ?>
    				<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    =======

    Second Part:

    <?php wp_reset_query(); endif; ?>
    
    	<?php $args = array(
    	'post_type'=> 'ShopResources',
    	'order'    => 'ASC'
    );
    query_posts( $args ); ?>
    				<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    Well something like that :wink:

  • Eric G.

    @Jonathan

    Tested out your code and did not get the results I was looking for still.

    I am using the author.php page and wanting to use two queries. I want one box to show their most recent posts and another to show their most recent Custom Post (ShopResources).

    Not only that but I need to have 'else' statements within the code if they do not have any posts for either of those yet. Thank you much for the help so far! Keep it coming. I can feel it I am almost there.

  • Philip John

    Where you have this;

    <?php
    $curauth = (get_query_var('author_name')) ? get_user_by('slug', get_query_var('author_name')) : get_userdata(get_query_var('author'));
    ?>

    Can you add a bit like this;

    <?php
    $curauth = (get_query_var('author_name')) ? get_user_by('slug', get_query_var('author_name')) : get_userdata(get_query_var('author'));
    echo "<!-- Author ID is ".$curauth." -->";
    ?>

    Then reload and check the source to make sure it's returning an ID correctly.

    Phil

  • Eric G.

    @jonathan

    Can you attach what you have for your code from top to bottom?

    The code you provided me still brings up the same things. All posts are showing for each author and only one Custom Post is showing and it's not filtered by author.

    @Phil

    Replaced that code and get the error

    Catchable fatal error: Object of class stdClass could not be converted to string in /home/rummagey/public_html/wp-content/themes/business-feature-child/author.php on line 11

  • Jonathan

    There you go Eric - this works - your template too :wink:
    You were right about the 'author' => id but I couldn't get the id to work - your problem too - lol, but i managed to figure it out :wink:

    <?php
    /**
     * The template for displaying Author Archive pages.
     *
     * @package WordPress
     * @subpackage Twenty_Eleven
     * @since Twenty Eleven 1.0
     */
    
    get_header();
    ?>
    
    <div id="content" class="narrowcolumn">
    	<!-- This sets the $curauth variable -->
    	<br/>
    	<?php
    		if (isset($_GET['author_name'])) :
    			$curauth = get_user_by('slug', $author_name);
    		else :
    			$curauth = get_userdata(intval($author));
    		endif;
    
    		$authorID = $curauth -> ID;
    	?>
    	<?php echo '<!--' . $authorID . '-->'; ?>
    	<div id="authormain">
    	<div id="authorleft">
    	<?php echo get_avatar($curauth -> user_email, '125');?>
    	</div>
    	<div id="authorcenter">
    	<h2><strong>Youth Worker Profile</strong><br/></h2>
    
    	<?php if ($curauth->first_name == '') { ?>
    	<h3><?php echo $curauth -> display_name;?> - Profile Yours?  <a href="http://youthministryresources.com/wp-admin/profile.php" target="_blank">Update it here</a></h3>
    	<?php } else {?>
    	<h3><?php echo $curauth -> first_name;?> <?php echo $curauth -> last_name;?></h3>
    	<?php }?>
    
    	<?php if ($curauth->user_description == '') { ?>
    	<?php } else {?>
    	<p><strong>About: </strong>
    	<?php echo $curauth -> user_description;?></p>
    	<?php }?>
    	</div>
    
    	<div id="authorright">
    	<div id="voteboxauthortotal"><div align="center">Total Votes Received<br/><?php wdpv_vote_result(false);?></div></div>
    	<?php if ($curauth->user_url == '') { ?>
    	No Website
    	<?php } else {?>
    
    	<div id="authorwebbox"><div align="center"><a href="<?php echo $curauth -> user_url;?>">Visit <?php echo $curauth -> first_name;?>'s Website</a></div></div>
    	<?php }?>
    	</div>
    	</div>
    
    	<br/>
    
    	<div id="clear"></div>
    
    	<?php if ($curauth->first_name == '') { ?>
    	<h2 align="center"><?php echo $curauth -> display_name;?>'s Contributions</h2>
    	<?php } else {?>
    	<h2 align="center"><?php echo $curauth -> first_name;?>'s Contributions</h2>
    	<?php }?>
    
    	<div id="postsbox">
    
    	<!-- The Loop -->
    	<h3 align="center">Free Youth Ministry Resources</h3>
    	<!-- Filter and Show Author's Free Resources -->
    	<ul>
    		<?php query_posts(array('post_type' => 'post', 'author' => $authorID, 'showposts' => 20));?>
    
    		<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
    		?>
    		<li>
    			<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title();?>"> <?php the_title();?></a>
    		</li>
    		<?php endwhile; else:?>
    		<p align="center">
    			<?php _e('This youth worker has not posted any free resources yet...');?>
    		</p>
    		<?php wp_reset_query();
    			endif;
    		?>
    	</ul>
    </div>
    <div id="postsbox">
    	<!-- Filter and Show Author's Shop Products -->
    	<h3 align="center">Resources For Sale</h3>
    	<?php query_posts(array('post_type' => 'ShopResources', 'author' => $authorID, 'showposts' => 20));?>
    
    	<ul>
    		<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
    		?>
    
    		<li>
    			<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title();?>"> <?php the_title();?></a>
    		</li>
    		<?php endwhile; else:?>
    		<h3><?php echo $curauth -> first_name;?>'s resources for sale in our shop:</h3>
    		<p>
    			<?php _e('This youth worker has no resources in our shop yet...');?>
    		</p>
    		<?php wp_reset_query();
    			endif;
    		?>
    	</ul>
    </div>
    <!-- End Loop -->
    </div> <?php get_sidebar();?>
    <?php get_footer();?>

    Hope that works. Seems to work on my side :wink:

  • Eric G.

    Well I well try some things with CustomPress and see if that changes anything. Maybe even set up a new Custom Post Type and see if that clears things out.

    Attaching a screenshot of the posts and authors just for kicks.

    Also, FYI. The code I am using now is exactly a copy and paste of the code above that you provided. So have not made any changes to it.

    Will follow-up with what I come up with.

  • Jonathan

    Okay had a look at your author profile - and checked your posts - and agree, kdklipfel is author of 1 post and you 2, but yours shows 1 and kdklipfel shows none.

    I just double checked on my super quick eric install setup - lol. And My dummy authors are working. Just added another page to dummy two and it shows up perfect.

    So possible area of breakdown. Custom post type is the only thing different between our setups. yours is ShopResources and mine is page. Yours is upper and lower case, mine is lowercase (try lowercase - shrugs?)

    Sorry Eric, I haven't a clue - custom post types can be a problem (the fact that you are using custompress) might have some role to play. But as long as it creates the post type normally it shouldn't be an issue. To test why not just create the post type the old fashioned way and add that code to functions.php - that will eliminate custompress from the equation:wink:

    EDIT: We posted at the same time - lol. Woot! Great stuff Eric - you had me scratching my head as to why??? it works on mine and not yours. Glad you got that sorted. It's always the stupid little things - lol.
    Thanks for the points :wink: Congrats on your lifetime membership :slight_smile:

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.