Support » Fixing WordPress » Excluding category from query_posts loop without having a fixed category ID

  • Resolved flint_and_tinder

    (@flint_and_tinder)


    Hi all,

    I’m trying to exclude a category from an embedded custom post type loop, but am a bit stuck.

    Ideally I’d like to not use the category ID as the theme maybe used across different sites and databases and I can’t guarantee the category I wish to exclude will always have an Id of x.

    My original loop code looks like this:

    <?php $temp_query = $wp_query; query_posts('post_type=projects&showposts=4&order=ASC');?>
        <?php if (have_posts()) : ?>
        <?php while (have_posts()) : the_post(); // grid loop. ?>

    The category I was the exclude is named ‘showreel’. I’ve found the following code elsewhere which I think might provide a solution but I’m not sure how to combine it with my existing loop:

    $exclude = get_cat_ID('News');
    	$q = 'cat=-'.$exclude;
    	query_posts($q);

    Is this something that can be done? If so, any pointers of how would be greatly appreciated.

    Many thanks,
    Adam.

Viewing 2 replies - 1 through 2 (of 2 total)
  • I think this will do what you want:

    <?php
    $temp_query = $wp_query;
    $exclude = get_cat_ID('showreel');
    query_posts("cat=-$exclude&post_type=projects&showposts=4&order=ASC");?>

    Be sure to use double-quotes around the argument string.

    A-ha! Double quotes.

    Thank you very much, it works like a charm.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Excluding category from query_posts loop without having a fixed category ID’ is closed to new replies.