Exclude category from wp_get_archives?

Is there a way to exclude a category from wp_get_archives? I am trying to show months in the sidebar, but I want to exclude posts that are not blogs.

$catID = get_cat_id('Projects');
$variable = wp_get_archives('type=monthly&show_post_count=1);
echo $variable;
+3
source share
9 answers

Use this if you want to include only specific categories for the wp_get_archive function in your functions.php file in 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'";

}
+5
source

You can write a filter in the functions.php file that changes the default behavior of the wp_get_archive function.

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

}
+2
source

, - PHP, .

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

}

taxonomy-name .

; , - wp_get_archives(). , ( ).

wp_get_archives(), functions.php, . , wp_get_archives(), :

<?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' );
?>
+1

wp_get_archives() - , (, , , ) " ": postbypost post by post, .

0

: WordPress > " /

Clean Archives Reloaded WordPress > Reloaded" WordPress 200:

// Get a simple array of all posts
$rawposts = get_posts( 'numberposts=-1&category=-4,-6,-7,-9' );
0

, get_categories . ; , .

0

pre_get_posts?

, is_author, is_home is_feed...

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

add_filter('pre_get_posts', 'exclude_stuff');

, - is_archive is_monthly

php :

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

Plugins .

0

:)

<?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();}
?>
0

There is no official way to do this. But I tried and tested a lot of code, 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 the original identifier of your message category and the code will work.

0
source

Source: https://habr.com/ru/post/1744516/


All Articles