$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>terms - $args for get_terms() to return ORDER BY FIELDS|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

terms - $args for get_terms() to return ORDER BY FIELDS

matteradmin10PV0评论

I want to use get_terms() for a taxonomy in such as way as to emulate MySQL's ORDER BY FIELD e.g., ...FIELD ('aaa','ccc','bbb','eee','ddd')

A var_dump snippet currently looks like:

object(WP_Term)#1861 (10) { ...["slug"]=> string(3) "eee" 
object(WP_Term)#1862 (10) { ...["slug"]=> string(3) "bbb" 
object(WP_Term)#1863 (10) { ...["slug"]=> string(3) "aaa" 
object(WP_Term)#1864 (10) { ...["slug"]=> string(3) "ccc" 
object(WP_Term)#1865 (10) { ...["slug"]=> string(3) "ddd" 

Have been reading the codex but I'm missing something. Here's what I have:

$args = array(
    'orderby'   => 'include', 
    'order'     => 'ASC',
    'include'   => array('aaa','ccc','bbb','eee','ddd'),
    'fields'    => 'all', 
);    

$roles = get_terms( 'role', $args );

As you can see above, the taxonomy in wp_mc_term_taxonomy is "role" and the slug's are 'aaa','ccc','bbb','eee','ddd' in wp_terms

What is the secret sauce? Thanks!

I want to use get_terms() for a taxonomy in such as way as to emulate MySQL's ORDER BY FIELD e.g., ...FIELD ('aaa','ccc','bbb','eee','ddd')

A var_dump snippet currently looks like:

object(WP_Term)#1861 (10) { ...["slug"]=> string(3) "eee" 
object(WP_Term)#1862 (10) { ...["slug"]=> string(3) "bbb" 
object(WP_Term)#1863 (10) { ...["slug"]=> string(3) "aaa" 
object(WP_Term)#1864 (10) { ...["slug"]=> string(3) "ccc" 
object(WP_Term)#1865 (10) { ...["slug"]=> string(3) "ddd" 

Have been reading the codex but I'm missing something. Here's what I have:

$args = array(
    'orderby'   => 'include', 
    'order'     => 'ASC',
    'include'   => array('aaa','ccc','bbb','eee','ddd'),
    'fields'    => 'all', 
);    

$roles = get_terms( 'role', $args );

As you can see above, the taxonomy in wp_mc_term_taxonomy is "role" and the slug's are 'aaa','ccc','bbb','eee','ddd' in wp_terms

What is the secret sauce? Thanks!

Share Improve this question asked Jan 15, 2019 at 19:01 breadwildbreadwild 3916 silver badges22 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

From WP_Term construct docs:

'include' (array|string) Array or comma/space-separated string of term ids to include. Default empty array.

Looks like you may need to change your includes array to hold ID's instead of slugs.

Post a comment

comment list (0)

  1. No comments so far