Forums

query_posts and order by date (10 posts)

  1. incomehero
    Member
    Posted 4 years ago #

    Ok, I want to show only one newest post on my first page, which is easy, but here's the trick.

    I want to show the newest post from all categories except 1 (ID=1).

    I thought that with query_posts this would be piece of cake, but it's not.. Here's what I use:

    <?php query_posts = ('showposts=1&cat=-1'); ?>

    This brings only one post, not including the cat number 1, but the post is THE OLDEST ONE, not the newest one...

    How can I order those posts in DESC order by date?

  2. incomehero
    Member
    Posted 4 years ago #

    The strangest thing is that if I rewrite that query in a different way, like:

    <?php query_posts = ('showposts=1&cat=2,3,4'); ?>

    Which includes all categories except the one with ID=1, everything is fine... But still, it would suck manually entering all those numbers every time you create a new directory...

  3. iridiax
    Member
    Posted 4 years ago #

  4. incomehero
    Member
    Posted 4 years ago #

    Strangely that doesn't work.

    Now I have:
    `<?php query_posts = ('showposts=1&cat=-1&orderby=date&order=DESC'); ?>

    I also tried &orderby=ID&order=DESC but still no luck.

    It seems that whenever you enter "cat=-1" ordering functions do not work. :/

  5. Peter Michael
    Member
    Posted 3 years ago #

    I had the same problem and the only solution I came up with is editing the file '/wp-includes/query.php' on line 1019 (WP 2.5.1), and I believe this is a bug in WordPress:


    if ( !empty($q['category__in']) || !empty($q['category__not_in']) || !empty($q['category__and']) ) {
    // -> THE FOLLOWING LINE CAUSES THE ORDERBY PROBLEM
    // $groupby = "{$wpdb->posts}.ID";
    }

    So far, this change didn't screw anything else up on my blog.

    HTH

  6. Peter Michael
    Member
    Posted 3 years ago #

    Edit: it works.

  7. Anonymous
    Unregistered
    Posted 3 years ago #

    <?php query_posts('cat=-1&orderby=date&order=DESC&showposts=1'); ?>

    cat goes before showposts

  8. Peter Michael
    Member
    Posted 3 years ago #

    @robphas

    Is this documented somewhere?

    Thanks

  9. feroz97
    Member
    Posted 3 years ago #

    How do I fix this problem ? please be specific as the previous fix by Flowdrops is not possible anymore.

  10. feroz97
    Member
    Posted 3 years ago #

    I fixed the issue in 2.7.1 by replacing query.php file in wp-includes with the query.php file from an older version of wordpress ie 2.6.5.
    Looks like there is a bug in the latest version.

Topic Closed

This topic has been closed to new replies.

About this Topic