WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Excluding category from query_posts loop without having a fixed category ID (3 posts)

  1. flint_and_tinder
    Member
    Posted 2 years ago #

    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.

  2. vtxyzzy
    Member
    Posted 2 years ago #

    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.

  3. flint_and_tinder
    Member
    Posted 2 years ago #

    A-ha! Double quotes.

    Thank you very much, it works like a charm.

Topic Closed

This topic has been closed to new replies.

About this Topic