Support » Fixing WordPress » Find Oldest Published Post

  • Edward Caissie

    (@cais)


    Theme Review Admin, The JellyBeen Man

    I have written a function that uses the single oldest published post, but it has been suggested it may be improved upon.

    Currently I am identifying the oldest post with this snippet:

    /* Get all posts */
    $all_posts = get_posts('post_status=publish&order=ASC');
    /* Get first post */
    $first_post = $all_posts[0];

    As this function is being used on every “public-facing page” is there a more efficient or less server intense method than the one I am using?

Viewing 3 replies - 1 through 3 (of 3 total)
  • esmi

    (@esmi)

    Forum Moderator

    As far as I can tell, that should only involve a single db query. Can’t see how you could reduce that.

    Edward Caissie

    (@cais)

    Theme Review Admin, The JellyBeen Man

    Thanks, I was just wondering if there may be another function, or variation, to find the oldest post. The code above was the most concise I could think of.

    Edward Caissie

    (@cais)

    Theme Review Admin, The JellyBeen Man

    OK, so this little bit of code has thrown me a bit of a curve ball …

    The above code finds the first post; short of running the query twice, any ideas on how to include an additional post_type, such as “page” in this query?

    I would like to avoid the following:

    /* Get all posts */
    $all_posts = get_posts('post_status=publish&order=ASC');
    /* Get first post */
    $first_post = $all_posts[0];
    
    wp_reset_query(); /* required? */
    /* Get all pages */
    $all_pages = get_posts('post_status=publish&order=ASC&post_type=page');
    /* Get first page */
    $first_page = $all_pages[0];

    The result I am looking for is the first published “entry”, whether it be a post or a page.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Find Oldest Published Post’ is closed to new replies.