0

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);
1

Yes, is doable.

I suggest you to add a custom field when some terms of that specific taxonomy are added to the post.

You should also delete that custom field when all the terms are removed from same post.

That can be done using 'set_object_terms'.

Please read the comments in code for more explaination:

add_action( 'set_object_terms', function( $object_id, $terms, $tt_ids, $taxonomy ){

  // Customize post type in next line according to your needs. I used 'category' as example
  if ( $taxonomy === 'category' ) {
    $post = get_post( $object_id );    
    if ( empty( $post ) ) return;

    // Customize post type in next line according to your needs. I used 'post' as example
    if ( $post->post_type !== 'post' ) return;

    // let's see if the post has some terms of this category,
    // because the hoook is fired also when terms are removed 
    $has_terms = get_the_terms( $object_id, $taxonomy );

    // here we see if the post already has the custom field
    $has = get_post_meta( $post->ID, "_category_relation_added", true );

    if ( ! $has && ! empty( $has_terms ) ) {
      // if the post has not the custom field but has some terms,
      // let's add the custom field setting it to current timestamp     
      update_post_meta( $post->ID, "_category_relation_added", time() );

    } elseif ( $has && empty( $has_terms ) ) {
      // on the countrary if the post already has the custom field but has not terms
      // (it means terms are all removed from post) remove the custom fields    
      delete_post_meta( $post->ID, "_category_relation_added" );
    }
  }   
}, 10, 4);

Don't forget to change your taxonomy and post type name.

After adding previous code to your functions.php you can order your posts using that custom field:

$args = array(
  // all your args here
  'meta_key' => '_category_relation_added',
  'orderby'  => 'meta_value_num',
  'order'    => 'DESC' // from more to less recent
);
$query = new WP_Query( $args );
  • Hi @G. M. Thank you for your answer. It works like a charm!! I have two questions: 1st. The old posts are not showing up because they do not have the meta_key, so should I add the new meta_key to those or can I still query them without it? 2nd. I tried to look for set_object_terms in the wordpress codex, but I could not find it, Is there a tutorial or documentation where I can learn more about this? – Gixty Mar 18 '14 at 19:15
  • ok I will do that. I just found a problem, when I remove the relationship, the date is not being deleted. So when I re-add the relationship, it has the same date as before instead of having a new date. – Gixty Mar 18 '14 at 19:57
  • post type is post and taxonomy is favorite – Gixty Mar 18 '14 at 20:05
  • This is what I use to set the terms wp_set_object_terms( $post_ID, $user_name, 'favorite', true ); and to remove them wp_remove_object_terms( $post_ID, $user_name, 'favorite' ); – Gixty Mar 18 '14 at 20:24
  • oh! So I cant have multiple instances of it? Each post can have multiple users that have favourited the post, yes! I need a different date for each, is that possible? – Gixty Mar 18 '14 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.