I am creating a page with a list of terms of a custom taxonomy. Since there are a lot of them, I have split them into pages.
This is what the query code looks like:
$mvp_posts_num = 12; //esc_html(get_option('mvp_posts_num'));
$paged = get_query_var('page')? get_query_var('page') : 1 ;
// WP_Term_Query arguments
$args = array(
'taxonomy' => array( 'city' ),
'number' => $mvp_posts_num,
'offset' => ((int)$paged - 1) * $mvp_posts_num,
'hide_empty'=> false,
'order' => 'ASC',
'orderby' => 'name',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'country_code',
'compare' => 'EXISTS',
),
),
);
$term_query = get_terms( $args );
And this is what the result looks like, for page 1:
[term_ids] => Array
717 , 718 , 719 , 720, 721, 722, 723, 724, 725, 726, 727, 728
the same query but with parameters offset=12, number=12 gives the following result
term_Ids
730, 717, 728, 727, 726, 725, 724, 723, 722, 721, 720, 719
For control, I took a query from Query Monitor and checked the SQL output:
For …/page/2 :
*SELECT DISTINCT t.term_id FROM ****_terms AS t INNER JOIN ****_termmeta ON ( t.term_id = ****_termmeta.term_id ) INNER JOIN ****_term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy IN (‘city’) AND ( ***_termmeta.meta_key = ‘country_code’ ) ORDER BY 1 ASC LIMIT 12, 12;
Result (list IDs) for page/2 must be this:
term_ids
729, 730, 731, 732, 733, 734, 735, 736, 737, 738, 739, 740
Compare the result that the “pure” SQL query gives and the one that is obtained via get_terms!
Not only the sequence, but also the sorting order do not match the query arguments.
On the 3rd and subsequent pages, the get_terms query still gives a randomly mixed sequence of terms from the 1st page.
I can’t explain this behavior. I also checked all the site theme and plugins code and didn’t find any filters that affect get_terms output.
Maybe someone has encountered this behavior of get_terms and can tell me what to do? Help needed!
1