I need to display a list of posts and order it by date. The date of when the taxonomy term relationship was created.
Is this possible? If so, how can I approach it?
Thank you.
EDIT - SOLVED!
Thank you to G. M. for helping me out.
Here is what I did:
For each set and remove action I give it a different meta_key with a user name as variable
The meta_key with variable
$meta_key = '_category_relation_added_' . $user_name;
To Remove term and meta
delete_post_meta($post_ID, $meta_key); wp_remove_object_terms( $post_ID, $user_name, $taxonomy );
To Add term and meta
wp_set_object_terms( $post_ID, $user_name, $taxonomy, true ); update_post_meta( $post_ID, $meta_key, time() );
Then we query the posts with WP_Query
$meta_key = '_category_relation_added_' . $user_name; $args = array( // all your args here 'meta_key' => $meta_key, 'orderby' => 'meta_value_num', 'order' => 'DESC' // from more to less recent ); $query = new WP_Query( $args );
UPDATE - To delete all postmeta related to the term when the term is deleted.
add_action( 'delete_term_taxonomy', function($tt_id) {
$term = get_term_by('term_taxonomy_id', $tt_id, 'favorite');
$user_name = $term->name;
$meta_key = "_category_relation_added_" . $user_name;
delete_post_meta_by_key( $meta_key );
}, 9, 1);