Support » Developing with WordPress » Taxonomy with Page attributes

  • Resolved Cezar Ayran

    (@ayrancd)


    I want to add a field link page-attributes to set that the taxonomy should be sorted by its number:1, 2, 3, 4… not alphabetically… Is there any way I can add this field? Or any better solution to sort it manually?

    This is my code:

    function custom_taxonmy(){ // FILE HAS TO BE taxonomy-category_tax.php
      $labels = array(
        'name'              => _x( 'Categories', 'taxonomy general name' ),
        'singular_name'     => _x( 'Category', 'taxonomy singular name' ),
        'search_items'      => __( 'Search Category' ),
        'all_items'         => __( 'All Categories' ),
        'parent_item'       => __( 'Parent Category' ),
        'parent_item_colon' => __( 'Parent Category:' ),
        'edit_item'         => __( 'Edit Category' ),
        'update_item'       => __( 'Update Category' ),
        'add_new_item'      => __( 'Add New Category' ),
        'new_item_name'     => __( 'New Category Name' ),
        'menu_name'         => __( 'Category' ),
    );
    
        register_taxonomy('pt_menu_category', 'pt_menu', //change this row
          array('labels' => $labels, 'public' =>  true, 'with_front' => false, 'hierarchical' =>  true, 'show_admin_column' => true, 'show_in_nav_menus' =>  true, 'has_archive' =>  true, 'rewrite' => 
          array('slug' => 'menus_cat'))); //change this row
    }
    add_action( 'init', 'custom_taxonmy'); 
Viewing 4 replies - 1 through 4 (of 4 total)
  • Joy

    (@joyously)

    When you say “number” what do you mean?

    You can sort by term_order by setting

    	'sort' => true,
    	'args' => array( 'orderby' => 'term_order' ),

    in the register_taxonomy call.
    See https://core.trac.wordpress.org/browser/tags/5.2/src/wp-includes/taxonomy.php#L2518 for the code where the sort is checked on set.
    On get, there is this comment:
    https://core.trac.wordpress.org/browser/tags/5.2/src/wp-includes/taxonomy.php#L696

    * It is possible to change the order that object_ids is returned by either
    * using PHP sort family functions or using the database by using $args with
    * either ASC or DESC array. The value should be in the key named 'order'.
    Moderator bcworkz

    (@bcworkz)

    You can add a field to the term edit screen through the “{$taxonomy}_edit_form” action, where {$taxonomy} is your taxonomy’s slug: pt_menu_category. Use the “edit_{$taxonomy}” action to save in term meta the field value passed in $_POST.

    You can then use the “pre_get_terms” action to set the “orderby” query var to “meta_value_num” and the “meta_key” query var to whatever key you used to save the value under. This influences any term query, so be sure the query is for your usage before changing query vars.

    Hi guys, tks.

    But I still don’t know how I can add this field called “order or whatever” and add a number to each item of my taxonomy and then when I get those posts I can sort by this “order” field…

    Guys… I just found a solution using this plugin https://wordpress.org/plugins/advanced-custom-fields/

    You can add fields to a taxonomy using it and then sort using

    foreach(get_categories(array('taxonomy' => 'pt_menu_category', 'hide_empty' => true, 'parent' => $mainCat->cat_ID, 'meta_key' => 'order', 'orderby' => 'meta_value', 'order' => 'ASC')) as $subCat){

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