#wordpress #sorting #custom-taxonomy #taxonomy-terms
#wordpress #sorting #custom-taxonomy #taxonomy-terms
Вопрос:
В WordPress есть задача для пользовательской таксономии для отображения терминов, отсортированных по значению мета-поля в формате DATETIME (Y-m-d H:i:s)
поле мета задается с помощью плагина ACF, и в базе данных все в порядке:
add_filter( 'get_terms_args', function ( $args, $taxonomies ) { global $pagenow; if ( is_admin() amp;amp; $pagenow == 'edit-tags.php' amp;amp; $taxonomies[0] == 'program_session' ) { $args['meta_query'] = [ 'relation' =gt; 'AND', 'conference' =gt; [ 'key' =gt; 'conference_id', 'value' =gt; get_active_conf_id(), 'compare' =gt; 'LIKE' ], 'session_start' =gt; array( 'key' =gt; 'program_session_start', 'type' =gt; 'DATETIME', 'compare' =gt; 'EXISTS', ), ]; $args['orderby'] = 'session_start'; $args['order'] = 'ASC'; } return $args; }, 10, 2 );
Условие фильтрации conference_id работает нормально, но сортировка-нет.
Попробовал вот так:
$term_query = new WP_Term_Query( [ 'taxonomy' =gt; ['program_session'], 'hide_empty' =gt; false, 'meta_query' =gt; [ 'relation' =gt; 'AND', 'conference' =gt; [ 'key' =gt; 'conference_id', 'value' =gt; get_active_conf_id(), 'compare' =gt; 'LIKE' ], 'session_start' =gt; array( 'key' =gt; 'program_session_start', 'type' =gt; 'DATETIME', ), ], 'orderby' =gt; 'session_start', 'order' =gt; 'ASC', ] ); echo 'lt;pregt;'; print_r($term_query-gt;terms);
everything is sorted correctly:
Array ( [0] =gt; WP_Term Object ( [term_id] =gt; 67 [name] =gt; Tasty breakfast [slug] =gt; tasty-breakfast [term_group] =gt; 0 [term_taxonomy_id] =gt; 67 [taxonomy] =gt; program_session [description] =gt; [parent] =gt; 0 [count] =gt; 0 [filter] =gt; raw ) [1] =gt; WP_Term Object ( [term_id] =gt; 65 [name] =gt; Good lunch [slug] =gt; good-lunch [term_group] =gt; 0 [term_taxonomy_id] =gt; 65 [taxonomy] =gt; program_session [description] =gt; [parent] =gt; 0 [count] =gt; 0 [filter] =gt; raw ) [2] =gt; WP_Term Object ( [term_id] =gt; 66 [name] =gt; Just dinner [slug] =gt; and-just-dinner [term_group] =gt; 0 [term_taxonomy_id] =gt; 66 [taxonomy] =gt; program_session [description] =gt; [parent] =gt; 0 [count] =gt; 0 [filter] =gt; raw ) )
temporarily solved the problem this way, but it’s not pretty!
add_filter( 'get_terms_args', function ( $args, $taxonomies ) { global $pagenow; if ( is_admin() amp;amp; $pagenow == 'edit-tags.php' amp;amp; $taxonomies[0] == 'program_session' ) { $args['menu_order'] = false; $args['ignore_term_order'] = false; $args['meta_query'] = [ 'conference' =gt; [ 'key' =gt; 'conference_id', 'value' =gt; get_active_conf_id(), 'compare' =gt; 'LIKE' ], ]; } return $args; }, 10, 2 ); add_filter( 'terms_clauses', function ( $pieces, $taxonomies, $args ) { global $pagenow, $wpdb; if ( is_admin() amp;amp; $pagenow == 'edit-tags.php' amp;amp; $taxonomies[0] == 'program_session' ) { $pieces['join'] .= ' INNER JOIN ' . $wpdb-gt;termmeta . ' AS tm ON t.term_id = tm.term_id '; $pieces['where'] .= ' AND tm.meta_key = "program_session_start"'; $pieces['orderby'] = ' ORDER BY tm.meta_value '; } return $pieces; }, 10, 3 );