• Resolved dshafer

    (@dshafer)


    OK, I give up. I’m stupid, blind or incompetent or WordPress’ documentation of its built-in template tags is abysmal. I’ve spent an hour looking for an answer to this question. (Where in the world is the full syntax of the query_posts function documented?)

    I need to retrieve and display the most recent post in a given category. I thought this should be easy because somehow I got the impression that getting just the most recent message is the default of query_posts(). So I tried:

    query_posts(‘category_name=Upcoming Lessons’)

    Nothing is returned but neither is an error generated. So I tried:

    query_posts(‘category_name=Upcoming Lessons&posts_per_page=1’);

    Same result. No display, no error.

    I have read everything I can find in the Codex on query_posts and have found zero help.

    Anyone care to point out where I’m doing something wrong?

Viewing 2 replies - 1 through 2 (of 2 total)
  • This works for me:

    <?php
        $args=array(
          'category_name'=>'cat1',
          'post_type' => 'post',
          'post_status' => 'publish',
          'posts_per_page' => 1,
          'caller_get_posts'=> 1
          );
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo 'List of Posts';
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
           <?php
          endwhile;
        }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    Thread Starter dshafer

    (@dshafer)

    Michael…..

    Two things.

    First, WOW. Thank you SO much for your quick and obviously informed reply. (I think I may actually understand your code and that’s frightening in itself!) ??

    Second, holy crap this stuff is complicated. I don’t mean this PHP is so complex in itself, but to figure out how to get here from where I was? That’s a world or two away. I was obviously wrong that using query_posts with some specific argument string would get this job done. That is both enlightening and disconcerting. Evidently, WordPress just below the surface of what’s built in is what I used to call back in my younger days as a real programmer, “non-trivial.”

    Thanks again. Now I have to rethink whether I really want to invest this much effort in learning WordPress when I already know other platforms.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Retrieving Only Latest Post from a Category’ is closed to new replies.