getarchives_where

最后更新于:2021-11-26 10:35:07

apply_filters( ‘getarchives_where’, string $sql_where, array $parsed_args )

Filters the SQL WHERE clause for retrieving archives.

参数

$sql_where

(string)
Portion of SQL query containing the WHERE clause.

$parsed_args

(array)
An array of default arguments.

源文件

文件: gc-includes/general-template.php

View on Trac

add_filter( 'getarchives_where', 'custom_archive_by_category_where' );
add_filter( 'getarchives_join', 'custom_archive_by_category_join' ); // You must add `join` to keep it works. Adding only `where` will return nothing

function custom_archive_by_category_where($x) {
    global $gcdb;
    $current_term_slug = get_query_var( 'news_category' );
 
    if (!empty($current_term_slug)) {
        $current_term = get_term_by('slug', $current_term_slug, 'news_category');
 
        if (is_gc_error($current_term) ) {
            return $x;
        }
 
        $current_term_id = $current_term->term_id;
 
        return $x . " AND $gcdb->term_taxonomy.taxonomy = 'news_category' AND $gcdb->term_taxonomy.term_id IN ($current_term_id)";
 
    }
 
    return $x;
}

function custom_archive_by_category_join( $x ) {
    global $gcdb;
    return $x . " INNER JOIN $gcdb->term_relationships ON ($gcdb->posts.ID = $gcdb->term_relationships.object_id) INNER JOIN $gcdb->term_taxonomy ON ($gcdb->term_relationships.term_taxonomy_id = $gcdb->term_taxonomy.term_taxonomy_id)";
}