WP_QUERY浏览次数最多的帖子,多个帖子类型,过去30天,不包括特定分类术语

时间:2013-03-03 作者:Chozen

好了,我差不多完成了90%,唯一一个不断被打破的部分是当我试图排除一个特定的分类术语时。

细分:

排序:(\'v\\u sort\'=>\'views\')这是通过流行的WP Postviews插件完成的。

帖子类型:帖子、视频、音乐、相册。

日期:最近30天。

Excluded Taxonomy Term: 分类法=内容,术语=indy。

迄今为止有效的代码。

<?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(); ?>
如何排除内容分类术语indy?顺便说一句,我是个初学者。

2 个回复
SO网友:birgire

您可以尝试:

$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\'
        )
    )
));
要排除indy 中的术语content 分类学

SO网友:Chozen

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(); ?>
结束

相关推荐

Admin user can't update WP

我有一个运行WP 3.3.2的站点。后端显示一个栏,上面写着“WordPress 3.5可用!请通知网站管理员。”有趣的是,我是用一个管理员用户登录的,我仔细检查了一下。尝试访问更新页面(位于wp admin/update core.php)时,我遇到以下错误:“您没有足够的权限访问此页面。”关于这里可能发生的事情有什么线索吗?谢谢