WP_Query most viewed posts, in multiple Post Types, last 30 days, excluding a specific taxonomy term

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:

Read More

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.

Related posts

Leave a Reply

2 comments

  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.

  2. 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(); ?>