0

I am using the following :

    $catargs = array(
        'meta_key'=> 'category_custom_order',
        'orderby' => 'meta_value_num',
        'order'=> 'ASC',

    ); 

but I can get the list of category sorted by the meta value key

image of the category in dashboard

unfortunately, it still listing the category by title instead.

Is there a better way to do this? thanks for helping.

The complete code:

    <?php 
    // Query by category and group
    $catargs = array(
        'meta_key'=> 'category_custom_order',
        'orderby' => 'meta_value_num',
        'order'=> 'DESC',

    ); 

    $categories = get_categories( $catargs );

    foreach ($categories as $category) { ?>
    <section id="<?php echo $category->slug; ?>" class="entry-category">
        <header class="entry-header">
            <h2 class="category-header"><?php echo $category->name;// Category title ?></h2>
        </header>
        <div class="entry-desc"><?php echo $category->description; ?></div>
        <?php // WP_Query arguments
        $args = array (
            'post_type'       => 'post',
            'cat'             => $category->cat_ID,
            'posts_per_page'  => '-1',
            'order'           => 'DESC',
            'orderby'         => 'title',
        );

        // The Query
        $query = new WP_Query( $args );

        // The Loop
        if ( $query->have_posts() ) {
            while ( $query->have_posts() ) {
                $query->the_post(); ?>
        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <header class="entry-header">
                <h3 class="entry-title">
                    <?php the_title(); ?>
                </h3>
            </header>
            <div class="entry-summary">
                <?php the_content(); ?>
            </div>
        </article>
        <?php }
            } else {
                // no posts found
            }
            // Restore original Post Data
            wp_reset_postdata(); ?>
    </section>
    <!-- #<?php echo $category->slug; ?> -->

I just want to sort the category by meta key value from the included image.

I am using the function bellow to create a custom field for my categories under the dashboard:

    /*
     * Add Custom Field To Category Form 
     */
    add_action( 'category_add_form_fields', 'category_form_custom_field_add', 10 );
    add_action( 'category_edit_form_fields', 'category_form_custom_field_edit', 10, 2 );

    function category_form_custom_field_add( $taxonomy ) {
    ?>
        <div class="form-field">
            <label for="category_custom_order">Custom Order</label>
            <input name="category_custom_order" id="category_custom_order" type="text" value="" size="10" aria-required="true" />
            <p class="description">Enter a custom order value.</p>
        </div>
    <?php
    }

    function category_form_custom_field_edit( $tag, $taxonomy ) {
        $option_name = 'category_custom_order_' . $tag->term_id;
        $category_custom_order = get_option( $option_name );
    ?>
        <tr class="form-field">
            <th scope="row" valign="top"><label for="category_custom_order">Custom Order</label></th>
            <td>
                <input type="text" name="category_custom_order" id="category_custom_order" value="<?php echo esc_attr( $category_custom_order ) ? esc_attr( $category_custom_order ) : ''; ?>" size="10" aria-required="true" />
                <p class="description">Enter a custom order value.</p>
            </td>
        </tr>
    <?php
    }

    /** Save Custom Field Of Category Form */
    add_action( 'created_category', 'category_form_custom_field_save', 10, 2 ); 
    add_action( 'edited_category', 'category_form_custom_field_save', 10, 2 );

    function category_form_custom_field_save( $term_id, $tt_id ) {
        if ( isset( $_POST['category_custom_order'] ) ) {
            $option_name = 'category_custom_order_' . $term_id;
            update_option( $option_name, $_POST['category_custom_order'] );
        }
    }//end

Now what I am looking for is how to order the category list by that custom field value ranging from 1 to 5 in asc order?

  • I don't understand your issue. Please add your code – Pieter Goosen Jul 1 '15 at 6:05
  • @PieterGoosen I have included the full code. Thank you for taking your time. – cmsdeployed Jul 1 '15 at 7:31
  • I dont think such feature is available currently. Categories does not have meta feature. – Nilambar Jul 1 '15 at 7:50
  • You are using invalid parameters in get_categories. Categories don't have custom fields neither can they even be sorted by any custom field. Your question now makes even less sense than before the edit – Pieter Goosen Jul 1 '15 at 8:20
  • 2
    Possible duplicate of Sort category page with custom field – T.Todua Jan 5 '17 at 20:55
2

Your question it make sense to me my friend but the get_categories don't have the option to order by "meta_value_num" and then order by custom fields.

What you can do is order after you get the array using the usort php function:

    $all_subcategories = array('parent' => $categoryID);

    $categories = get_categories( $all_subcategories );

    function order_categories($a, $b) {
    if ($a->term_id == $b->term_id) {
        return 0;
    }
        return ($a->term_id < $b->term_id) ? -1 : 1;
    }

    usort($categories, "order_categories");

It work fine for me. I used ACF to add the category order field but is fine the way you made it and should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.