Forums

[resolved] query_posts orderby=date not working when i exclude a category (9 posts)

  1. classna
    Member
    Posted 1 year ago #

    I am trying to display the latest 5 posts on my homepage using the following code:

    query_posts('cat=-9&showposts=5&orderby=date&order=desc' );

    I am excluding category 9 in this case. It works fine but the orderby=date is not working. I want to display the newest post first here. If you remove the category filter, i.e. cat=-9 the orderby=date works but with the category exclusion filter, it doesn't. I am stuck here and searched everywhere for a possible fix. Please help.

    P.S. i also disabled all the plugins if that might have caused this but still no luck.

  2. classna
    Member
    Posted 1 year ago #

    am i the only one having this issue?

  3. keesiemeijer
    moderator
    Posted 12 months ago #

    try it also with "posts_per_page". "showposts" is deprecated as of Version 2.1 in favor of "posts_per_page". "DESC" is the default so leave it out. Do you have multiple loops on the page? try wp_reset_query above the query:

    wp_reset_query();
    
    query_posts('cat=-9&posts_per_page=5&orderby=date' );
  4. classna
    Member
    Posted 12 months ago #

    Tried with posts_per_page and wp_reset_query but it still does not work. This is the only loop in my index page. I do have some plugins that show posts from various categories in the sidebar. Disabling the plugins didn't solve this issue..

  5. keesiemeijer
    moderator
    Posted 12 months ago #

    try it with this kind of query:

    // The Query
    $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $the_query = new WP_Query( 'cat=-9&posts_per_page=5&orderby=date&paged=' . $page );
    
    // The Loop
    while ( $the_query->have_posts() ) : $the_query->the_post();
  6. classna
    Member
    Posted 12 months ago #

    Thanks keesiemeijer for your help but this didn't work either.. i think its the category exclusion preventing it to sort. If i get rid of the category filter, the date sort works.

  7. keesiemeijer
    moderator
    Posted 12 months ago #

    Same problems here: http://wordpress.org/support/topic/query_posts-and-order-by-date?replies=10

    I just read the "order by" parameter defaults to "date" so you can maybe leave it out: http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

  8. classna
    Member
    Posted 12 months ago #

    Never mind, It was not working on my local machine.. Works on my live website. Still don't know why it was not working locally. here'z the code:
    query_posts('&cat=-9&orderby=date&order=DESC&posts_per_page=5' );

    Thanks keesiemeijer for all your help.

  9. keesiemeijer
    moderator
    Posted 12 months ago #

    No problem. Glad you got it working on the live website.

Topic Closed

This topic has been closed to new replies.

About this Topic