Support » Fixing WordPress » query_posts not work with minus sign

  • 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");
    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");
    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

Viewing 5 replies - 1 through 5 (of 5 total)
  • 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..


    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?

    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 ) ) );

    Hi t31os_, thanks for your explanation.
    query_posts( array( 'cat' => '-47' ) );
    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.

    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.

    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'));

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘query_posts not work with minus sign’ is closed to new replies.