Last active
March 26, 2019 18:58
-
-
Save petenelson/7feef670cf03d87376620adc3ee31861 to your computer and use it in GitHub Desktop.
wp_update_post() resetting term order
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Sample code to demonstrate a possible bug in core where | |
// wp_update_post() is resetting the term_order. | |
global $wp_taxonomies; | |
// Let core set the term_order field automatically. | |
$wp_taxonomies['category']->sort = true; | |
$post_id = wp_insert_post( | |
[ | |
'post_title' => 'Test Title', | |
'post_content' => 'Test Content', | |
'post_status' => 'publish', | |
] | |
); | |
$term_names = [ | |
'E) First Term', | |
'Z) Second Term', | |
'A) Third Term ', | |
]; | |
$term_ids = []; | |
foreach ( $term_names as $term_name ) { | |
$existing_term = term_exists( $term_name, 'category' ); | |
if ( is_array( $existing_term ) ) { | |
$term_ids[] = $existing_term['term_id']; | |
} else { | |
$new_term = wp_insert_term( $term_name, 'category' ); | |
if ( is_array( $existing_term ) ) { | |
$term_ids = $new_term['term_id']; | |
} | |
} | |
} | |
wp_set_post_terms( $post_id, $term_ids, 'category', false ); | |
// Verify the terms are in sorted by the term_order. | |
$terms = wp_get_post_terms( $post_id, 'category', [ 'orderby' => 'term_order' ] ); | |
foreach ( $terms as $term ) { | |
var_dump( $term->name ); | |
} | |
$post = get_post( $post_id ); | |
var_dump( 'Calling wp_update_post()' ); | |
// Update the post and then get the terms again. | |
wp_update_post( | |
[ | |
'ID' => $post_id, | |
'post_title' => 'New Test Title', | |
] | |
); | |
// Now the terms come back alphabetical, but they should still be by term order. | |
$terms = wp_get_post_terms( $post_id, 'category', [ 'orderby' => 'term_order' ] ); | |
foreach ( $terms as $term ) { | |
var_dump( $term->name ); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/var/www/html/wp-content/plugins/sample-code/includes/test:79: | |
string(13) "E) First Term" | |
/var/www/html/wp-content/plugins/sample-code/includes/test:79: | |
string(14) "Z) Second Term" | |
/var/www/html/wp-content/plugins/sample-code/includes/test:79: | |
string(13) "A) Third Term" | |
/var/www/html/wp-content/plugins/sample-code/includes/test:82: | |
string(24) "Calling wp_update_post()" | |
/var/www/html/wp-content/plugins/sample-code/includes/test:96: | |
string(13) "A) Third Term" | |
/var/www/html/wp-content/plugins/sample-code/includes/test:96: | |
string(13) "E) First Term" | |
/var/www/html/wp-content/plugins/sample-code/includes/test:96: | |
string(14) "Z) Second Term" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment