Exclude Category From wp_get_archives?

2019-05-14 18:00发布

问题:

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;

回答1:

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'";

}


回答2:

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)";

}


回答3:

Ran into this problem in a project but never found a solution online -- mine isn't the prettiest PHP, but it does the trick.

This is a play off the filter suggested by Katie, which I ran across in few support forums as well. This goes in your functions.php:

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;

    $categories = get_terms( 'taxonomy-name', 'orderby=id' );
    $includeIds;
    $i = 0;
    foreach($categories as $category) {
        if($i != 0) $includeIds .= ',';
        $includeIds .= $category->term_id;
        $i++;
    } 


    return $x . " AND $wpdb->term_taxonomy.taxonomy = 'taxonomy-name' 
    AND $wpdb->term_taxonomy.term_id IN ($includeIds)";

}

In the second function, swap taxonomy-name for the name of your actual custom taxonomy.

All the IDs of terms in your custom taxonomy are captured in a string; the rest operates the same as the original function -- only that list of categories from your custom taxonomy are included in the wp_get_archives() list. You can also tweak the code to exclude them as well (first example above).

If you only want one instance of the wp_get_archives() list to do this, just skip the top two lines of code in your functions.php that apply the filters. Then, when you use the wp_get_archives() tag, apply the filters before it, and remove them afterwards:

<?php   
add_filter( 'getarchives_where', 'customarchives_where' );
    add_filter( 'getarchives_join', 'customarchives_join' );

    wp_get_archives(); 

    remove_filter( 'getarchives_where', 'customarchives_where' );
    remove_filter( 'getarchives_join', 'customarchives_join' );
?>


回答4:

wp_get_archives() does not have a mechanism to exclude based on category -- it's purely for time-based archives (yearly, monthly, daily, weekly) or "every post" archives: postbypost or post by post ordered by post title.



回答5:

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' );


回答6:

You might want to look in to get_categories and lean towards your own custom solution. While this may cost you a little more time and work; you will indeed get the upshot of having full control over what you're trying to achieve.



回答7:

Place the code below just after This code is working for me already :)

<?php
if ( $wp_query->is_archive ){
$wp_query->query_vars["cat"] = 14; // only the category that you want to inlcude
$wp_query->query_vars["posts_per_page"] = 10; // for number of posts you want
$wp_query->get_posts();}
?>


回答8:

Can you use a filter hook on pre_get_posts instead?

I know something like this works for is_author, is_home, and is_feed...

function exclude_stuff($query) { 
    if ( $query->is_author) {
        $query->set('cat', '-4, -142');
    }
    return $query;
}

add_filter('pre_get_posts', 'exclude_stuff');

depends on whether you can do it for something like is_archive or is_monthly

You would drop that in a php file with a plugin header:

<?php
/*
 * Plugin Name: exclude some stuff
 * Description: blah
 * Author: blah
 * Plugin URI: blah
 * Version: 0.9
 * =======================================================================
*/
   Put the function here
?>

Then upload it to your Plugins directory and activate it.



回答9:

There isn't an official way of doing this. But i tried and tested a lot of code block and only this one worked for me.

    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;
    $include = 'your_category_id'; // category id to include
    return $x . " AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id IN ($include)";
}

Replace your_category_id with an original id of your post category and the code will work.