Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

The setup:

  • custom post type of 'attorney'
  • custom taxonomy of 'specialty', which is registered against the 'attorney' post type
  • custom post type of 'practice-area'

For every specialty, there is a matching practice area. However, there are many more practice areas than there are specialties.

Ideally, when viewing a practice area (say, Bankruptcy), I'd be able to list attorneys who have Bankruptcy as a specialty.

I know I could use the posts2posts plugin, and create a connection between the attorney post type and the practice-area post type. However, that would mean essentially setting an attorney's specialty twice (once as a taxonomy term, and once as a posts2posts connection). Is there a way to somehow make a connection between the a specialty taxonomy term and the relevant practice-area post type? I could simply assume that names and/or slugs will match up, but that's a pretty hacky/fragile solution.

I'd ideally like to connect regular 'category' terms to the various 'practice-area' post types as well, to list relevant blog posts on the individual practice area pages (and vice versa).

Suggestions?

share|improve this question

2 Answers 2

I've dealt a similar requirement and so far the best way to do that is by using a custom field to save the related term id.

That means for each 'practice-area' post type, there will be a 'specialty-term-id' custom field with the 'specialty' term id as value.

Here the action hook to create the term for each post

add_action( 'save_post', 'update_related_term');
function update_related_term($post_id) {
$post_type_as_taxonomy = array('practice-area');
$post = get_post( $post_id );
if(in_array($post->post_type, $post_type_as_taxonomy) && $post->post_status=='publish'){
    $term_args['name'] = $post->post_title;
    $term_args['slug'] = $post->post_name.'';
    $term_id = get_post_meta($post_id, $post->post_type.'-term-id', true);
    if($term_id){
        $term = wp_update_term( $term_id, $post->post_type.'-term', $term_args );
    } else {
        $term = wp_insert_term( $term_args['name'], $post->post_type.'-term', $term_args );
        $meta_status = add_post_meta($post_id, $post->post_type.'-term-id', $term['term_id'], true);
    }

}
}

and the action to delete the term on each post delete

add_action('admin_init', 'codex_init');
function codex_init() {
if (current_user_can('delete_posts')){
    add_action('before_delete_post', 'delete_related_term', 10);
}
}
function delete_related_term($post_id) {
$post_type_as_taxonomy = array('practice-area');
$post = get_post( $post_id );
if (in_array($post->post_type, $post_type_as_taxonomy)) {
    $term = get_post_meta($post_id, $post->post_type.'-term-id', true);
    wp_delete_term( $term, $post->post_type.'-term');
}
}

Note that i used 'practice-area' as the custom post type and 'practice-area-term' as the related taxonomy.

Hope this help

share|improve this answer

if i understand you correctly then when you are on the Bankruptcy 'practice-area'... 'Bankruptcy' will be the term that you want to query in the 'speciality' taxonomy.

you could try putting this on your 'single-practice-area.php' template:

$args = array( 'post_type'=>'attorney',
    'tax_query' => array(

        array(
            'taxonomy' => 'specialty',
            'terms' => get_query_var('practice-area'), //get current practice-area's name
            'field' => 'slug',
        )
    )
);

$attorneys = get_posts($args);

global $post;
$tmp_post = $post;

if( $attorneys ) : echo "Attorneys<ul>";
foreach( $attorneys as $post ) : setup_postdata($post); ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; 
$post = $tmp_post; 
echo "</ul>";
endif; ?>
share|improve this answer
    
I realize I could do that, and just assume that the slugs will match up. However, they could easily not match up, thus the desire to manually create that connection. –  tnorthcutt Mar 6 '12 at 22:48
    
true enough. i've actually been trying to link taxonomies lately too and as far as i can tell there isn't a way to do it. though if you don't go by post slug, then i'm stumped how the 'Bankruptcy' post is supposed to know that it needs to show a specific term. maybe you could set it in post meta as a fallback if the slug isn't correct? –  helgatheviking Mar 6 '12 at 23:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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