Exclude posts with taxonomy term from appearing on home page query

I am really confused about this one, I am usually pretty good at working out basic queries but I can’t seem to work this one out.

Please see below my index.php query. Now it is not your basic query because I have a special drop down menu to filter posts on the home page.

Read More

I have a taxonomy set up called Site Position, and my term is called Home Page Exclude. This taxonomy is assigned to my posts.

In an array slug style, I use this in my WP_querys.. 'position' => 'home-page-position',

But the term I wish to exclude, as a slug is called home-page-exclude

My question is, from this slightly more complex query below.. how can I exclude all posts with this term??

<?php

        if ($_REQUEST['sort'] == 'oldest')

            $order = "&orderby=date&order=ASC";

        else if ($_REQUEST['sort'] == 'views')

            $order = "&meta_key=views&orderby=meta_value_num&order=DESC";

        else

            $order = "&orderby=date&order=DESC";

    ?>

    <form method="post" id="order">

        <?php $url = get_bloginfo('url');?>
        <select name="sort" onchange='location.href="<?php echo $url ?>?sort="+this.value'>
            <option value="newest" <?php if (isset($_REQUEST['sort']) && $_REQUEST['sort'] == 'newest' ){ ?> selected="selected" <?php } ?> >Sort by Newest</option> <!-- default -->
            <option value="oldest" <?php if (isset($_REQUEST['sort']) && $_REQUEST['sort'] == 'oldest' ){ ?> selected="selected" <?php } ?> >Sort by Oldest</option>
            <option value="views" <?php if (isset($_REQUEST['sort']) && $_REQUEST['sort'] == 'views' ){ ?> selected="selected" <?php } ?> >Sort by Most Viewed</option>
        </select>

    </form>



<?php wp_reset_query(); $posts = query_posts($query_string . $order); ?>

<?php if (have_posts()) : ?>

<?php while (have_posts()) : the_post(); ?>

This is what I have tried but doesn’t work.

<?php

        $tax_query = array (
            array(
                'taxonomy' => 'position',
                'terms' => 'home-page-exclude',
                'field' => 'slug',
                'operator' => 'NOT IN'
            )
        );

        if ($_REQUEST['sort'] == 'oldest')

            $order = "&orderby=date&order=ASC";

        else if ($_REQUEST['sort'] == 'views')

            $order = "&meta_key=views&orderby=meta_value_num&order=DESC";

        else

            $order = "&orderby=date&order=DESC";

    ?>

    <form method="post" id="order">

        <?php $url = get_bloginfo('url');?>
        <select name="sort" onchange='location.href="<?php echo $url ?>?sort="+this.value'>
            <option value="newest" <?php if(isset($_REQUEST['sort']) && $_REQUEST['sort'] == 'newest' ){ ?> selected="selected" <?php } ?> >Sort by Newest</option> <!-- default -->
            <option value="oldest" <?php if(isset($_REQUEST['sort']) && $_REQUEST['sort'] == 'oldest' ){ ?> selected="selected" <?php } ?> >Sort by Oldest</option>
            <option value="views" <?php if(isset($_REQUEST['sort']) && $_REQUEST['sort'] == 'views' ){ ?> selected="selected" <?php } ?> >Sort by Most Viewed</option>
        </select>

    </form>

</div>

<?php wp_reset_query(); $posts = query_posts( $query_string . $order . $tax_query ); ?>

<?php if (have_posts()) : ?>

<?php while (have_posts()) : the_post(); ?>

Any ideas would be very awesome thanks.

Related posts

Leave a Reply