Exclude Category From wp_get_archives?

2019-05-14 17:26发布

Is there any way to exclude a category from wp_get_archives? I'm trying to show the months in the sidebar, but I want to exclude the posts that are not blog entries.

$catID = get_cat_id('Projects');
$variable = wp_get_archives('type=monthly&show_post_count=1);
echo $variable;

9条回答
ゆ 、 Hurt°
2楼-- · 2019-05-14 18:08

Use this if you want to include only specific categories for wp_get_archive function in your functions.php of your theme directory

add_filter( 'getarchives_where', 'customarchives_where' );
add_filter( 'getarchives_join', 'customarchives_join' );

function customarchives_join( $x ) {

    global $wpdb;

    return $x . " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)";

}

function customarchives_where( $x ) {

    global $wpdb;

    $includes= '14'; // category id to include
    return $x . " AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id = '$includes'";

}
查看更多
smile是对你的礼貌
3楼-- · 2019-05-14 18:08

There are different ways to work with category archives: WordPress › Support » Limit archives to category / date archives for category

I use Clean Archives Reloaded WordPress › Clean Archives Reloaded « WordPress Plugins and exclude categories around line 200:

// Get a simple array of all posts
$rawposts = get_posts( 'numberposts=-1&category=-4,-6,-7,-9' );
查看更多
别忘想泡老子
4楼-- · 2019-05-14 18:11

You can write a filter in your functions.php file which will change wp_get_archive function's default behavior.

add_filter( 'getarchives_where', 'customarchives_where' );
add_filter( 'getarchives_join', 'customarchives_join' );

function customarchives_join( $x ) {

    global $wpdb;

    return $x . " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)";

}

function customarchives_where( $x ) {

    global $wpdb;

    $exclude = '1'; // category id to exclude

    return $x . " AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id NOT IN ($exclude)";

}
查看更多
登录 后发表回答