I am faced with a thorny problem and can't seem to find the solution in the wealth of community knowledge on the web. I have two custom posts types "Person" and "Activities". I use posts 2 posts (plugin) to link Persons with their various Activities and vice-versa.

I have Taxonomies which apply to Activities. They describe areas of practice and interest. Using them, I'd like to be able to get a list of Persons who partake in Activities tagged with "Cranes", for example. Where "Cranes" is part of an Area of interest in my tag-like custom taxonomy.

I suspect the most practical way to go about it would be to apply the taxonomy to both post-types and keep them in sync from the linked Activities to the Persons automatically. Is there a way to do that?

Kind regards,

EDIT: My p2p_register_connection_type is as follows

function ghunige_connection_types() {
    p2p_register_connection_type( array(
        'name' => 'profile_to_activity',
        'from' => 'ghunige_profile',
        'to' => 'ghunige_activity',
        'reciprocal' => true,
        'title' => array( 'from' => __('Participates in', 'ghunige_dir_domain'), 'to' => __('Participants', 'ghunige_dir_domain') )

    ) );

I think there are 2 solutions for you problem. The first is rely on P2P functions like each_connected (see this as example) and retrieve the connected 'Person' after running a taxonomy query for 'Activities'.

Maybe this solution can be easier, faster and straightforward.

Second solution is what you says in your question: apply the taxonomy to both post types.

To sync taxonomy terms you can hook into action 'added_term_relationship' action

   add_action( 'added_term_relationship', 'sync_my_tax', 20, 2);

   function sync_my_tax ( $object_id, $tt_id ) { 
     $post = get_post($object_id);
     if ( $post->post_type == 'activities' ) {
       global $wpdb;
       $taxonomy = $wpdb->get_var("SELECT taxonomy FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = " . $tt_id);
       if ( $taxonomy != 'your_taxonomy' ) return; // put here your tax name
       $related = p2p_type( 'person_to_activities' )->get_related( $post );  
       if ( ! empty($related) ) { foreach ( $related as $person ) {
         $wpdb->insert( $wpdb->term_relationships, array( 'object_id' => $person->ID, 'term_taxonomy_id' => $tt_id ) );
       } }

I cannot assure p2p_type call in this code is right because you don't post your p2p_register_connection_type() call.

Also note, that all the code is untested, so don't use on production site before you have fully tested it.

  • I can't seem to adapt the code to my p2p_register_connection_type. Neither can I find the 'added_term_relationship' hook in the documentation. Who adds it: Wordpress or P2P? – Evren Kiefer Aug 30 '13 at 13:02
  • @Evren when you need info on code, best documentation is code itself. See wp-includes/taxonomy.php line 2210 – gmazzap Aug 30 '13 at 19:11
  • Lesson learned, @G. M. I think I understand what the above code does and how. I have double, triple and quadruple checked my variables and tested, tested, tested the code. It doesn't work, yet. I have a couple follow-up questions: - What is the taxonomy's name? Is it the slug, the query string or the proper name that goes in register_taxonomy()? - Is there a "taxonomy" table in the DB or is it supposed to be the name of the taxonomy also? I don't have access to our database at this time. – Evren Kiefer Sep 20 '13 at 9:28
  • @Evren taxonomy's name is the one goes in register_taxonomy(). Taxonomy in wordpress are handled with 3 different tables: $wpdb->terms, $wpdb->term_taxonomy (the 1 save terms data, the second the terms related to taxonomy). Then there is the $wpdb->term_relationships the contains the taxonomy - posts associations. I can't test the code because I've no P2P installed, so I can't help you more than this... – gmazzap Sep 20 '13 at 9:40

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.