• Resolved gf

    (@gf)


    Hello.

    Currently I’m trying to display posts by the author’s id and according to query_post this code should work, could chance i’m missing something.

    Can somebody help

    get_header();
    ?>
    
        <div>
      Latest News
          <ul>
    		<?php $polly_current_author = the_author_meta('ID');
    
            //testing purposes
    		echo $polly_current_author;
    
    		query_posts("cat=3&author=$polly_current_author"); ?>
    <?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('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>
    
    <!-- End Loop -->
    
        </ul>
        </div>
    
    <?php get_footer(); ?>

    thanks

Viewing 15 replies - 1 through 15 (of 17 total)
  • Does your version actually have a number in place of ID.
    <?php $polly_current_author = the_author_meta('ID');

    As is, ID isn’t a valid value, it should be numeric, 1, 2, 3, so on..

    Thread Starter gf

    (@gf)

    thanks for the reply, first one i’ve had from 3 posts.

    Doesn’t the_author_meta(‘ID’) get the value.

    because the echo $polly_current_author; currently displays the correct author ID.

    Sorry getting my functions confused (i was incorrect in my last statement).

    What’s the problem you’re having with the code?

    Thread Starter gf

    (@gf)

    Its not limiting the list of post from cat 3 to just a specific author.

    This is been used on a profile page for each author, so i would like to show only there latest news post and not other authors post on the profile page.

    Not that it should matter, but does using this help.

    query_posts('cat=3&author='.$polly_current_author);

    Thread Starter gf

    (@gf)

    no doesn’t seem to want to work, very frustrating.

    Thread Starter gf

    (@gf)

    redid it to this.

    It is definitely echoing the correct author ID its just not passing it into the query.

    <?php
     $polly_current_author = the_author_meta('ID'); // assign the variable as current category
     $query= 'author=' .$polly_current_author. '&cat=3'; // concatenate the query
     query_posts($query); // run the query
     echo $polly_current_author; // check data is been passed
     ?>

    the_author_meta() will return the author ID if called from within the loop— after this while ( have_posts() ) and before this endwhile; If used outside the loop you have to provide it with the author ID. You are using it outside the loop, so it isn’t going to do what you want. I’m not sure why its giving you the correct author ID. I tested it on my server and it returns author ID == 0. With the correct author ID the rest of your code seems to work fine.

    Take a look at Author Templates. There are examples of ways to get the author ID from outside of the Loop.

    Thread Starter gf

    (@gf)

    so

    <?php
    if(get_query_var('author_name')) :
        $curauth = get_userdatabylogin(get_query_var('author_name'));
    else :
        $curauth = get_userdata(get_query_var('author'));
    endif;
    ?>
    
        <div>
      Latest News
    
    		 <?php
     $polly_current_author = $curauth->ID;// assign the variable as current category
     $query= 'author=' .$polly_current_author. '&cat=3'; // concatenate the query
     query_posts($query); // run the query
     echo $polly_current_author; // check data is been passed
     ?>
    
     <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><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>
    
    <!-- End Loop -->
    
        </ul>
        </div>

    Oh, I meant to mention that the_author_meta() echoes data. You don’t want that. You’d want get_the_author_meta() to hold the data in a variable. See the codex referenced in my previous post.

    I can’t figure out why you are getting the correct ID. Your new code doesn’t work for me either. If I run this (cat changed to something that works on my system):` $polly_current_author = the_author_meta(‘ID’); // assign the variable as current category
    echo $polly_current_author.’
    ‘;
    $query= ‘author=’ .$polly_current_author. ‘&cat=53’; // concatenate the query
    echo $query.’
    ‘;
    query_posts($query); // run the query
    echo $polly_current_author.’
    ‘; // check data is been passed`

    I get this: `0
    author=&cat=53`

    Thread Starter gf

    (@gf)

    <?php
    if(get_query_var('author_name')) :
        $curauth = get_userdatabylogin(get_query_var('author_name'));
    else :
        $curauth = get_userdata(get_query_var('author'));
    endif;
    ?>
    
        <div>
      Latest News
    
    		 <?php
     $query= 'author=' . $curauth->ID. '&cat=3'; // concatenate the query
     query_posts($query); // run the query
     ?>

    more like this even. I’m not displaying this data with the author temple page.

    Thread Starter gf

    (@gf)

    this has sorted it

    <?php
     $polly_current_author = get_the_author_meta('ID'); // assign the variable as current category
     $query= 'author=' .$polly_current_author. '&cat=3'; // concatenate the query
     query_posts($query); // run the query
     echo $polly_current_author; // check data is been passed
     ?>

    I needed get_the_author_meta.

    Not sure why its working for me though i’m getting the author ID, honest.

    thanks

    That last part is getting closer. Start off by replacing this:

    if(get_query_var('author_name')) :
        $curauth = get_userdatabylogin(get_query_var('author_name'));
    else :
        $curauth = get_userdata(get_query_var('author'));
    endif;

    with this:`
    $curauth = get_userdatabylogin(‘admin’);`

    You should be able to manually switch this between authors by editing the code, right?

    Okay. Now to make it dynamic. In the Authors Page example they used the PHP $_GET variable to switch between authors. You are going to have to do something similar. get_query_var() isn’t going to help you here. It works after building a query, not before the query as you are using it.

    Work on that. I have to go out for awhile. Good luck.

    I’d be careful with that function. That really should not be working unless you are running it inside another loop. Are you running it inside another loop? If you are, you may be corrupting the parent loop.

    Thread Starter gf

    (@gf)

    yeah, i’m running is in another loop.

    but i have shortened it and its not doing anything strange to the parent.

    <?php
     $polly_current_author = get_the_author_meta('ID'); // assign the variable as current category
     ?>
           <?php
    
           $recent = new WP_Query('author=' .$polly_current_author. '&cat=3'); while($recent->have_posts()) : $recent->the_post();?>

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘Display latest post by author’ is closed to new replies.