1

Here is the code I have. It is showing 100 custom post known as campaigns. I need it to only show the current users. So author/joe should only show joe's campaigns.

  <?php

   if(isset($_GET['author_name'])) :

    $curauth = get_userdatabylogin($author_name);

    else :

    $curauth = get_userdata(intval($author));

     endif;

     ?>




<h2>Campaigns by <?php echo $curauth->nickname; ?>:</h2>



<?php
$args = array(
'posts_per_page' => 100,
'post_type' => 'campaigns'

  );
query_posts($args);
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><?php _e('No posts by this author.'); ?></p>


<?php endif; ?>
  • Don't use query_posts, ever. Don't even use a custom query. Use pre_get_posts to add your custom post type to the author pages – Pieter Goosen Jan 22 '15 at 5:44
3

Do not use query_posts, and for that matter any custom query to replace the main query. It is always problematic and it creates more problems that solving it.

Use the main query and make use of pre_get_posts to alter the main query as needed.

To solve your issue, remove the query_posts line, this is how author.php should look.

<?php

   if(isset($_GET['author_name'])) :

        $curauth = get_userdatabylogin($author_name);

    else :

        $curauth = get_userdata(intval($author));

    endif;

?>

<h2>Campaigns by <?php echo $curauth->nickname; ?>:</h2>

<?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><?php _e('No posts by this author.'); ?></p>


<?php endif; ?>

Add the following in your functions.php file

add_action( 'pre_get_posts', function ( $q ) {

    if( !is_admin() && $q->is_main_query() && $q->is_author() ) {

        $q->set( 'posts_per_page', 100 );
        $q->set( 'post_type', 'campaigns' );

    }

});

This should solve your issue

  • AMAZING! Thank you. There was a syntax error. I had to add an opening php tag before the if statement in the loop to make it work. Is that correct? – JediTricks007 Jan 25 '15 at 19:43
  • You are correct, sorry, missed that. Copy and paste error :-). Thanks for your feedback. I will correct that for future reference. Enjoy :-) – Pieter Goosen Jan 25 '15 at 19:46
0

i dont see the problam in using WP_Query...
it has an author parameter.

$cuser_id = get_current_user_id();

$args = array(
    'post_type'         =>  'campaigns',
    'posts_per_page'    =>  100,
    'author'            =>  $cuser_id,

);
$the_query = new WP_Query($args);

if ($the_query->have_posts()) {
    while ($the_query->have_posts()) {
        $the_query->the_post();

        // BUILD AND DISPLY CAMPAIGN DATA
        $pid    =   get_the_ID();
        $ptitle =   get_the_title();
        $plink  =   get_permalink();

        echo '<li><a href="'.$plink.'">'.$ptitle.'</a></li>';

    }

} else {
    echo 'You have published any campaigns yet';
}
wp_reset_postdata();

Hope this helps.

  • 1
    Don't use custom queries in place of the main query. The main query can be correctly modified with pre_get_posts – Pieter Goosen Jan 22 '15 at 6:52
  • there is no need to use a filter. he can wrap this in a function and embed it inside the author.php page in any position he needs. – Sagive SEO Jan 22 '15 at 8:44
  • Hey I am getting Parse error: syntax error, unexpected T_VARIABLE for the line of code $the_query = new WP_Query($args); – JediTricks007 Jan 23 '15 at 17:42
  • yee - forgot one ";" after the args array - revised answer if you hadent fixed this already then just copy - paste – Sagive SEO Jan 24 '15 at 5:01
  • I got another syntax error and had to add a ; after THEME_NAME) . Now I just see title over and over again on author.php profiles. – JediTricks007 Jan 25 '15 at 1:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.