WordPress.org

Ready to get started?Download WordPress

Forums

query_posts not work with minus sign (6 posts)

  1. ZDJL505
    Member
    Posted 4 years ago #

    Hi,
    I want to exclude certain category (e.g for category_ID=47) from loop by using code (with minus sign):
    query_posts($query_string . "&cat=-47");
    or
    query_posts("cat=-47");
    But both of them are not work to exclude.

    Just for checking, if I try using code (without minus sign:
    query_posts($query_string . "&cat=47");
    or
    query_posts("cat=47");
    Both of them are work to show posts only from category_ID=47.
    How to solve this problem?

    My server configuration:
    WPMU 2.9.2
    PHP 5.2.9
    MySQL 5.0.90-community-log
    Apache 2.2.11

  2. Mark / t31os
    Moderator
    Posted 4 years ago #

    Check if query_string has something in it first.

    If you're passing in an empty string(you might be), then you end up with a malformed query string which looks like..

    &cat=47
  3. ZDJL505
    Member
    Posted 4 years ago #

    Hi t31os_, thanks for the reply.
    I have check variable $query_string is empty.
    FYI, if I use query_posts("cat=-47"); it's still can not exclude category_ID=47 from the loop.

    How to solve this problem?

  4. Mark / t31os
    Moderator
    Posted 4 years ago #

    Maybe try as an array then..

    query_posts( array( 'cat' => '-47' ) );

    Or even with single quotes instead (not that it should make a difference)..

    query_posts( 'cat=-47' );

    Or just switch to category__not_in instead.

    query_posts( array( 'category__not_in' => array( 47 ) ) );

    http://codex.wordpress.org/Function_Reference/query_posts#Category_Parameters

  5. ZDJL505
    Member
    Posted 4 years ago #

    Hi t31os_, thanks for your explanation.
    query_posts( array( 'cat' => '-47' ) );
    and
    query_posts( 'cat=-47' );
    Both not work.

    It work with
    query_posts( array( 'category__not_in' => array( 47 ) ) );

    Why in my case query_posts does not recognize minus sign? Is this problem with my database or with my query.php/classes.php? Is this typical of WPMU that can not exclude posts in query_posts?

    Any way, my problem is solved event it remaining some questions.

  6. Mark / t31os
    Moderator
    Posted 4 years ago #

    Not sure why it's not working, however the cat parameter passes it values onto category__in and category__not_in anyway, so it's perfectly fine to use them in place of cat.

    Eg.
    query_posts('cat=-1'); is the same as query_posts(array('category__not_in' => '-1'));
    query_posts('cat=1'); is the same as query_posts(array('category__in' => '1'));
    query_posts('cat=-1,2'); is the same as query_posts(array('category__not_in' => '-1','category__in'=>'2'));

Topic Closed

This topic has been closed to new replies.

About this Topic