1

OK so I'm about 90% done, the only part that keeps breaking is when I try to exclude a specific taxonomy term.

Break down:

Sorting: ('v_sort' => 'views') This is being done via the popular WP-Postviews plugin.

Post Types: post, videos, music, albums.

Date: The last 30 days.

Excluded Taxonomy Term: Taxonomy = content, Term = indy.

The code of what works thus far.

<?php

    // Create a new filtering function that will add our where clause to the query
    function filter_where( $where = '' ) {
        // posts in the last 30 days
        $where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
        return $where;
    }

    add_filter( 'posts_where', 'filter_where' );

    // The Query
    $the_query = new WP_Query( array(
        'posts_per_page' => '5',
        'v_sortby' => 'views', 
        'post_type' => array( 'post', 'music', 'videos', 'albums' ) )
     );
    remove_filter( 'posts_where', 'filter_where' );
    // The Loop
    while ( $the_query->have_posts() ) :
        $the_query->the_post(); ?>

I'm doing stuff here...

    <?php endwhile;

    /* Restore original Post Data 
     * NB: Because we are using new WP_Query we aren't stomping on the 
     * original $wp_query and it does not need to be reset.
    */
    wp_reset_postdata(); ?>

How would I exclude the content taxonomy term indy? I'm a beginner btw.

2 Answers 2

1

You can try:

$the_query = new WP_Query( array(
    'posts_per_page' => '5',
    'v_sortby' => 'views', 
    'post_type' => array( 'post', 'music', 'videos', 'albums' ),
    'tax_query' => array(
        array(
            'taxonomy' => 'content',
            'field' => 'slug',
            'terms' => array( 'indy' ),
            'operator' => 'NOT IN'
        )
    )
));

to exclude the indy term in the content taxonomy.

3
  • Doesn't work, it breaks the query.
    – Chozen
    Mar 4, 2013 at 4:29
  • ok, I think I had one extra ), the syntax should be ok now ;-)
    – birgire
    Mar 4, 2013 at 7:15
  • any luck so far?
    – birgire
    Mar 7, 2013 at 11:26
0

Solved.

<?php

// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
    // posts in the last 30 days
    $where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
    return $where;
}

add_filter( 'posts_where', 'filter_where' );

$the_query = new WP_Query( array(
    'posts_per_page' => '5',
    'v_sortby' => 'views', 
    'post_type' => array( 'post', 'music', 'videos', 'albums' ),
    'tax_query' => array(
        array(
            'taxonomy' => 'content',
            'field' => 'slug',
            'terms' => array( 'indy' ),
            'operator' => 'NOT IN'
        )
    )
));
remove_filter( 'posts_where', 'filter_where' );
// The Loop
while ( $the_query->have_posts() ) :
    $the_query->the_post(); ?>


<a class="widget-post-title" href="<?php the_permalink() ?>" rel="bookmark">
<?php the_title(); ?></a>

<a class="thumbnails-link" href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"> </a>

<?php endwhile;

/* Restore original Post Data 
 * NB: Because we are using new WP_Query we aren't stomping on the 
 * original $wp_query and it does not need to be reset.
*/
wp_reset_postdata(); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.