获取相对于当前查询的类别计数

Jua*_*gel 10 php wordpress

我正在使用WordPress,在搜索页面上,我需要获取该类别中的帖子数量并将其显示在名称旁边.像这样

Cat 1(3)Cat 2(1)Cat 3(18)....

目前我正在使用这些get_categories()函数,它们为您提供了一个对象并使用了$cat->count().这会抓住术语中的帖子总数,但我只需要从当前查询中的帖子中获取该计数.我pre_get_posts用来更新查询.有人知道这样做的方法吗?

这是我目前的代码块

foreach ( get_categories() as $cat ) {
    if ( $cat->parent === 0 ) {
        $checked = ( in_array( $cat->slug, $check ) ) ? 'checked' : '';

        printf( '<input id="%1$s" %4$s type="checkbox" name="category[%1$s]"><label for="%1$s" >%2$s ( %3$d )</label><br>',
            $cat->slug,
            $cat->name,
            $cat->count,
            $checked
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的pre_get_posts行动:

add_action( 'pre_get_posts', 'breed_search_query' );
function breed_search_query( $query ) {
$cats   = ( isset( $_GET['category'] ) ) ? implode( ',', array_keys( $_GET['category'] ) ) : null;
$search = ( isset( $_GET['s'] ) ) ? sanitize_text_field( $_GET['s'] ) : null;

    if ( ! is_admin() && $query->is_main_query() && is_search() ) {
        $query->set( 'post_type', 'post' );
        $query->set( 'posts_per_page', 8 );
        $query->set( 's', $search );
        $query->set( 'category_name', $cats );
    }
}
Run Code Online (Sandbox Code Playgroud)

Pie*_*sen 6

必须说好的和有趣的问题.不幸的是,你不会得到这个get_categories,所以你需要另一种方法.

我们将如何做到这一点:

  • 创建一个非常精简的自定义查询,以从与查询匹配的所有帖子中获取所有帖子ID

  • 获取附加到帖子的所有类别

  • 遍历帖子中的所有帖子和类别,并构建一个数组,其中类别名称为键,并将ID作为值

  • 循环遍历此新数组,并显示类别名称,并计算和显示该特定键(类别)的帖子数量

代码:

(注意:下面的代码是未测试的,并在真皮休闲PHP 5.4+需要由于新的数组语法([]))

$cats   = ( isset( $_GET['category'] ) ) ? implode( ',', array_keys( $_GET['category'] ) ) : null;
$search = ( isset( $_GET['s'] ) ) ? sanitize_text_field( $_GET['s'] ) : null;

$args = [
    'post_type'     => 'post',
    'nopaging'      => true, // Gets all posts
    's'             => $search,
    'category_name' => $cats,
    'fields'        => 'ids', // Makes the query extremely lean, excellent for performance, only get post ID's
];
$q = get_posts( $args );

// Define our $category_array which will hold the category name/post ID's array
$category_array = [];

// Define our $category_info array which will hold the category name as key and the object as value
$category_info = [];

foreach ( $q as $post ) {
    // Get the categories attached to a post
    $categories = get_the_category( $post );

    foreach ( $categories as $category ) {
        // Create our array which will hold the category name as key and post ID's as values
        $category_array[$category->name][] = $post;
        // Create an array to hold the category objects. Use names as keys and objects as values
        $category_info[$category->name] = $category;
    } // endforeach $categories
} // endforeach $wp_query->posts
wp_reset_postdata();

// Sort the array keys alphabetical. You can change or remove this as necessary
ksort( $category_array );

foreach ( $category_array as $k=>$v ) {

    $category_slug = $category_info[$k]->slug;
    $category_parent = $category_info[$k]->parent;

    $checked = ( in_array( $category_slug, $check ) ) ? 'checked' : ''; // Just make sure that $check is defined somewhere

    // Build our string
    printf ( '<input id="%1$s" %4$s type="checkbox" name="category[%1$s]"><label for="%1$s" >%2$s ( %3$d )</label><br>', // I have not modified anything in this line
        $category_slug, // Holds the category slug
        $k, // Holds the category name
        count( $v ), // Counts and return the post count
        $checked
    );
} // endforeach $category_array
Run Code Online (Sandbox Code Playgroud)