按自定义字段排序Get_Terms()

时间:2013-10-08 作者:Howdy_McGee

我想像排序页面一样排序我的类别。我在分类法中添加了一个名为“顺序”的额外字段,但如何根据该值对术语进行排序?

添加和保存我的订单值:

/** Add New Field To Category **/
function extra_category_fields( $tag ) {
    $t_id = $tag->term_id;
    $cat_meta = get_option( "category_$t_id" );
?>
<tr class="form-field">
    <th scope="row" valign="top"><label for="meta-order"><?php _e(\'Category Order\'); ?></label></th>
    <td>
        <div id="catOrder">
            <input type="text" name="cat_meta[order]" size="3" style="width:5%;" value="<?php echo (isset($cat_meta[\'order\'])) ? $cat_meta[\'order\'] : \'0\'; ?>" />
        </div>
        <span class="description"><?php _e(\'Categories are ordered Smallest to Largest\'); ?></span>
    </td> 
</tr>
<?php
}
add_action(\'protax_edit_form_fields\',\'extra_category_fields\');  

/** Save Category Meta **/
function save_extra_category_fileds( $term_id ) {
    if ( isset( $_POST[\'cat_meta\'] ) ) {
        $t_id = $term_id;
        $cat_meta = get_option( "category_$t_id");
        $cat_keys = array_keys($_POST[\'cat_meta\']);
            foreach ($cat_keys as $key){
            if (isset($_POST[\'cat_meta\'][$key])){
                $cat_meta[$key] = $_POST[\'cat_meta\'][$key];
            }
        }
        //save the option array
        update_option( "category_$t_id", strip_tags($cat_meta) );
    }
}
add_action ( \'edited_protax\', \'save_extra_category_fileds\');
通常,我可以检索此值,例如:

$categories = get_categories(array(\'taxonomy\' => \'protax\'));
foreach($categories as $cat){
    $tmp = get_option(\'category_\'.$cat->cat_ID);
    echo $tmp[\'order\'];
}
是否可以按此自定义值订购?是否有更好的方法手动排序我的类别?

2 个回复
最合适的回答,由SO网友:Howdy_McGee 整理而成

SOLUTION - Updated: 03/03/2015 - 感谢/u/G.M。

下面在术语编辑页面中添加了一个新字段,并将该值保存到未使用的字段中(暂时不使用)term_group 然后可用于订购条款的字段。在以下挂钩(操作)中,您需要替换TAXONOMY_SLUG 用你真正的分类学鼻涕虫。

/**
 * Append Fields To Term Edit Page
 * @param Term Object $term
 * @param string $taxonomy
 */
function term_order_field( $term, $taxonomy ) {
  ?>
    <tr class="form-field">
        <th scope="row" valign="top">
            <label for="meta-order"><?php _e( \'Category Order\' ); ?></label>
        </th>
        <td>
            <input type="text" name="_term_order" size="3" style="width:5%;" value="<?php echo ( ! empty( $term->term_group ) ) ? $term->term_group : \'0\'; ?>" />
            <span class="description"><?php _e( \'Categories are ordered Smallest to Largest\' ); ?></span>
        </td> 
    </tr>
  <?php
}
add_action( \'TAXONOMY_SLUG_edit_form_fields\', \'term_order_field\', 10, 2 );

/**
 * Save Term Order
 * @param int $term_id
 */
function save_term_order( $term_id ) {
    global $wpdb;

    if( isset( $_POST[\'_term_order\'] ) ) {
        $wpdb->update( 
            $wpdb->terms,
            array(
                \'term_group\' => $_POST[\'_term_order\']
            ),
            array( 
                \'term_id\'    => $term_id
            )
        );
    }
} // END Function
add_action( \'edited_TAXONOMY_SLUG\', \'save_term_order\' );

Add Term Order cell to Term List

/**
 * Add Column To Show \'Term Order\' Field
 * @param array $columns
 * @return array $columns
 */
function term_order_header( $columns ) {
    $columns[\'order\'] = \'<center>Order</center>\';
    return $columns;
}
add_filter( \'manage_edit-TAXONOMY_SLUG_columns\', \'term_order_header\' );

/**
 * Give \'Term Order\' Column A Value `term_group`
 * @param string $empty
 * @param string $col_name
 * @param int $term_id
 * @return string
 */
function term_order_header_val( $empty = \'\', $col_name, $term_id ) {
    if( isset( $_GET[\'taxonomy\'] ) && \'order\' == $col_name ) {
        $term = get_term( $term_id, $_GET[\'taxonomy\'] );
        return "<center>{$term->term_group}</center>";
    } else {
        return \'0\';
    }
}
add_filter( \'manage_TAXONOMY_SLUG_custom_column\', \'term_order_header_val\', 10, 3 );

Hook Documentation

  • action {$taxonomy}_edit_form_fields
  • action edited_{$taxonomy}
  • filter manage_{$screen->id}_columns
  • filter manage_{$this->screen->taxonomy}_custom_column

    /** Add New Field To Category **/
    function extra_category_fields( $tag ) {
        $t_id = $tag->term_id;
        $cat_meta = get_option( "category_$t_id" );
    ?>
    <tr class="form-field">
        <th scope="row" valign="top"><label for="meta-order"><?php _e(\'Category Order\'); ?></label></th>
        <td>
            <div id="catOrder">
                <input type="text" name="cat_meta[order]" size="3" style="width:5%;" value="<?php echo (isset($cat_meta[\'order\'])) ? $cat_meta[\'order\'] : \'0\'; ?>" />
            </div>
            <span class="description"><?php _e(\'Categories are ordered Smallest to Largest\'); ?></span>
        </td> 
    </tr>
    <?php
    }
    add_action(\'protax_edit_form_fields\',\'extra_category_fields\');  
    
    /** Save Category Meta **/
    function save_extra_category_fileds( $term_id ) {
        global $wpdb;
        if ( isset( $_POST[\'cat_meta\'] ) ) {
            $t_id = $term_id;
            $cat_meta = get_option( "category_$t_id");
            $cat_keys = array_keys($_POST[\'cat_meta\']);
                foreach ($cat_keys as $key){
                if (isset($_POST[\'cat_meta\'][$key])){
                    $cat_meta[$key] = $_POST[\'cat_meta\'][$key];
                }
            }
            update_option( "category_$t_id", $cat_meta );
            $wpdb->update($wpdb->terms, array(\'term_group\' => $cat_meta[\'order\']), array(\'term_id\'=>$term_id));
        }
    }
    add_action ( \'edited_protax\', \'save_extra_category_fileds\');
    
    称之为:

    $procats = get_categories(array(\'taxonomy\' => \'protax\', \'hide_empty\' => 0, \'orderby\' => \'term_group\'));
    
    Edit - 您还可以使用此筛选器在管理面板中对其进行排序:

    function change_term_order( $orderby, $args, $taxonomies ) {
        if ( is_admin() && \'protax\' !== $taxonomies[0] )
            return $orderby;
    
        $orderby = \'term_group\';
        $args[\'order\'] = \'ASC\';
        return $orderby;
    }
    add_filter( \'get_terms_orderby\', \'change_term_order\', 10, 3 );
    

SO网友:HelpNeeder

What about this:

$terms_hearing = merge_tax_and_custom_field(
    get_terms([
        \'taxonomy\'   => \'hearing\',
        \'hide_empty\' => false,
    ]),
    \'abbr\'
);

sort_tax_by_custom_field($terms_hearing, \'abbr\');

// function will allow to merge taxonomy result with custom taxonomy terms
function merge_tax_and_custom_field($terms, $field)
{
    // merge custom field into original taxonomy object
    foreach ($terms as $index => $term) {
        // grabbing custom field data
        $data = get_option(\'taxonomy_term_\' . $term->term_id)[$field];

        // adding data field into term object as property
        $terms[$index]->{$field} = $data;
    }

    return $terms;
}

// sort field by custom field value
function sort_tax_by_custom_field($terms, $field)
{
    usort($terms, function ($a, $b) {
        return strcmp($a->abbr, $b->abbr);
    });

    return $terms;
}
结束