• Resolved stuffren

    (@stuffren)


    I have created custom category Product Categories through Custom Post Type UI and the basic settings of Product Categories are as follows:

    Taxonomy Slug: product_category

    Plural Label*: Product Categories

    Singular Label *: Product Category

    Attach to Post Type *: products

    In CPT’s Product Categories settings, the Sort option is currently set to true.

    In the wordpress backend Product Categories management interface, I have created 32 categories and all categories are currently displayed in the category list.

    In functions.php, I have added sorting functionality to both the new category form and the edit category form in the product_category admin interface. The code is as follows:



    /*****

    ?* [EN]Add sorting settings to both the New Category form and Edit Category form in the product_category admin interface.

    [Zh]在 product_category 管理界面的新建分类表单和编辑分类表单中添加排序设置

    ?*/

    // [EN]Add a sort field to the new category form. [Zh]在新建分类的表单中添加排序字段

    function add_category_sort_field($taxonomy) {

    ? ? ?>

    ? ? <div class="form-field term-sort-wrap">

    ? ? ? ? <label for="tag-sort">sort</label>

    ? ? ? ? <input name="term-sort" id="tag-sort" type="number" value="" size="40">

    ? ? ? ? <p> Set the sorting order of this category, the smaller the number, the higher the sorting order.</p>

    ? ? </div>

    ? ? <?php

    }

    add_action('product_category_add_form_fields', 'add_category_sort_field');

    // [EN]Add a sorting field in the edit category form. [Zh]在编辑分类的表单中添加排序字段

    function edit_category_sort_field($term, $taxonomy) {

    ? ? // Get the sort value of the current category 获取当前分类的排序值

    ? ? $sort_value = get_term_meta($term->term_id, 'term_sort', true);

    ? ? ?>

    ? ? <tr class="form-field term-sort-wrap">

    ? ? ? ? <th scope="row"><label for="tag-sort">sort</label></th>

    ? ? ? ? <td>

    ? ? ? ? ? ? <input name="term-sort" id="tag-sort" type="number" value="<?php echo esc_attr($sort_value); ?>" size="40">

    ? ? ? ? ? ? <p class="description"> Set the sorting order of this category, the smaller the number, the higher the sorting order.</p>

    ? ? ? ? </td>

    ? ? </tr>

    ? ? <?php

    }

    add_action('product_category_edit_form_fields', 'edit_category_sort_field', 10, 2);

    //[EN] Save the value of the sort field? 保存排序字段的值

    function save_category_sort_meta($term_id) {

    ? ? if (isset($_POST['term-sort'])) {

    ? ? ? ? update_term_meta($term_id, 'term_sort', intval($_POST['term-sort']));

    ? ? }

    }

    add_action('created_product_category', 'save_category_sort_meta', 10, 2);

    add_action('edited_product_category', 'save_category_sort_meta', 10, 2);

    Then, in functions.php, I add a display of individual category sorting for the category list in the product_category admin interface.

    The code is as follows:

    /*****

    * [EN] Add a single sorted category display for the category list in the product_category admin interface.

    * [Zh]为 product_category 管理界面的分类列表加入单个分类排序的展示

    ?*****/

    // [EN]Add a custom “Sort By” column to the category list. [ZH]添加自定义的“排序”列到分类列表

    function add_sort_column_to_category_list($columns) {

    // Insert a new “Sort By” column after the “Name” column. 在“名称”列之后插入一个新的“排序”列

    ? ? $columns['term_sort'] = 'sort';

    ? ? return $columns;

    }

    add_filter('manage_edit-product_category_columns', 'add_sort_column_to_category_list');

    // Populate the data in the “Sort By” column. 填充“排序”列中的数据

    function fill_sort_column_in_category_list($content, $column_name, $term_id) {

    ? ? if ($column_name == 'term_sort') {

    ? ? ? ? // Get the sort value of the current category 获取当前分类的排序值

    ? ? ? ? $sort_value = get_term_meta($term_id, 'term_sort', true);

    ? ? ? ? if ($sort_value !== '') {

    ? ? ? ? ? ? $content = intval($sort_value); // Make sure the value is an integer 确保值为整数

    ? ? ? ? } else {

    ? ? ? ? ? ? $content = '—'; // If no sort is set, show dashes.如果没有设置排序,则显示破折号

    ? ? ? ? }

    ? ? }

    ? ? return $content;

    }

    add_filter('manage_product_category_custom_column', 'fill_sort_column_in_category_list', 10, 3);

    // [EN]Make the “Sort By” column support sorting.[Zh]让“排序”列支持排序功能

    function make_sort_column_sortable($sortable_columns) {

    ? ? $sortable_columns['term_sort'] = 'term_sort';

    ? ? return $sortable_columns;

    }

    add_filter('manage_edit-product_category_sortable_columns', 'make_sort_column_sortable');

    /*****

    *[EN] Ensure that the categories are sorted correctly by the term_sort field.

    ?* [ZH]确保分类按照 term_sort 字段正确排序

    ?*/

    ?function sort_category_by_sort_meta($query) {

    //Ensure that it is only applied on the product_category admin page in the backend.

    //确保仅在后台的 product_category 管理页面上应用

    ? ? if (!is_admin() || !isset($query->query_vars['taxonomy']) || $query->query_vars['taxonomy'] != 'product_category') {

    ? ? ? ? return;

    ? ? }

    ? ? // Check for 'orderby=term_sort' in URLs.检查 URL 中是否有 'orderby=term_sort'

    ? ? if (isset($query->query_vars['orderby']) && $query->query_vars['orderby'] == 'term_sort') {

    ? ? ? ? // Force sorting with term_sort metadata 强制使用 term_sort 元数据进行排序

    ? ? ? ? $query->set('meta_key', 'term_sort');

    ? ? ? ? $query->set('orderby', 'meta_value_num'); // 使用元数据数字进行排序

    ? ? ? ? $query->set('order', isset($query->query_vars['order']) ? $query->query_vars['order'] : 'ASC'); // Default ascending order 默认升序

    ? ? }

    }

    add_action('pre_get_terms', 'sort_category_by_sort_meta');

    I’ve created 32 categories and set ordering values from 1-32 for each of them. Screenshots of some of the categories and sort values are shown below:

    Current problem: Clicking on the sort button for a category sorts it, but the sort result for the category is garbled.

    For example, there are currently 32 categories, when I click on the ascending button, there is no 1.2.3…30.31.32 sorting, but a random chaotic sorting with sort values 1-32 is displayed; when I click on the descending button, there is no 32-1 sorting, but a random chaotic sorting with sort values 1-32 is displayed as well;

    What I need is that when I click on the ascending button, the sorting is according to 1.2.3…30.31.32; when I click on the descending button, the sorting is according to 32.31.30 …. .3.2.1 way to sort;

    I’ve tried many ways but can’t solve this problem, I hope CPT UI official friend, help me to see what is the problem?

    How should I solve this problem?

    Looking forward to a reply from the CPT UI team! Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.