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

标签 php wordpress

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

类别 1 (3) 类别 2 (1) 类别 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
        );
    }
}

这是我的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 );
    }
}

最佳答案

必须说的好有趣的问题。不幸的是,您不会让它与 get_categories 一起工作,因此您需要另一种方法。

这是我们将如何做到的:

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

  • 获取帖子的所有类别

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

  • 遍历这个新数组,显示类别名称,并计算并显示该特定键 ( category ) 的帖子数量

代码:

(注意:以下代码未经测试,由于新的数组语法 ([]))

$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

关于php - 获取相对于当前查询的类别计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31372094/

相关文章:

javascript - 使用 JavaScript 切换联系表单字段

php - PHP 的 mail() 函数附件支持在 Windows 上是否有点损坏?

php - 使用 PHP 从 MySQL 数据库中获取数据,以表格形式显示以供编辑

php - 在一个数组中从 mysql 检索多个结果

jquery - 使用 jquery 将动画添加到 bootstrap Tooltip

wordpress - 如何在 WordPress 中更新 get_stylesheet_directory_uri

php - 获取子数组键php

javascript - 使用 Start Bootstrap "Agency"主题提交表单时出错

mysql - wordpress 中的 siteurl 更改(在数据库中更改),但链接仍指向先前的 url

wordpress - 在 wordpress 中悬停时子环菜单无法点击