0

I added a sequence field to a taxonomy with ACF. I read here that wp_list_categories() creates an instance of the WP_Term_Query class.

I can orderby the category listing based on the ACF field with the following code

<?php
/**
 * The Template for displaying project archives category list at single hierarchy level.
 *
 *
 * @author      mZoo
 * @package     Projects/Templates
 * @version     1.0.0
 */

$args = array('taxonomy' => 'project-category',
              'title_li' => '',
              'depth' => 1,
              'style' => '',
              'echo' => 0,
              'meta_key' => 'project_category_sequence',
              'orderby' => 'meta_value'
            );

              ?>

<?php $categories = wp_list_categories($args);

if ( $categories ) {
    printf( '<div class="category-grid">%s</div>', $categories );
}

This only returns categories for which this field is populated.

What I want is to:

  1. also return the categories for which the ACF field isn't set
  2. order them by title

Is this a job for a meta_query?

UPDATE

I have tried this approach:

'meta_query' => [
      'relation' => 'OR',
      'project_sequence_clause' => [
          'key'     => 'project_category_sequence',
          'value' => 'EXISTS'
      ],
      'title_clause' => [
          'key'     => 'title',
          'value' => 'EXISTS'
      ],
  ],
  'orderby' => [
      'project_sequence_clause' => 'ASC',
      'title_clause' => 'ASC'
  ],

It seems that WP_Term_Query->parse_orderby() requires a string and not an array.

I think that there may be something useful in WP_Term_Query->parse_orderby_meta(), but for now I'm just going to populate the project_category_sequence field manually as a workaround, and make it a required field so new categories will have it by default.

Your Answer

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

Browse other questions tagged or ask your own question.