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.

I have a website we're building with 1600 'Business Listings' which are made as custom post types. Our problem is that, while there only needs to be one 'editor' of each post/company, there are sometimes more than one user(employees) associated with the post/company.

The biggest reason for this is grouping users with certain businesses, which in turn gets used for email newsletters, and just overall contact info (only seen in the backend by the admins).

SO. Is there a way to associate more than one user to a post? Basically out client wants to be able to go into a company's profile (aka custom post) and see all of the employees attached to that company.

Thoughts? Multiple author-to-post plugin or hack we can do?

share|improve this question
    
Googling wordpress multiple authors per post led me to wpbeginner.com/plugins/… Is there anything there you can use? (Linked from that post is the Co-Authors plugin.) –  Pat J Aug 9 '13 at 16:45
    
txfx.net/wordpress-plugins/multiple-authors this plugin works well –  Dave Burns Aug 9 '13 at 16:48
1  
Since you're building a company directory and want to list all their employees, I don't think you want them to be WordPress users. Instead, use a custom post type for employees and map them with the Posts 2 Posts plugin. This way you can associate x employees with a specific company. Note: The plugin basically also supports multiple authors, using posts-users relationships. –  swissspidy Aug 10 '13 at 12:15

1 Answer 1

up vote 2 down vote accepted

Unless all these employees will need to login to Wordpress, it may make much more sense to implement employees as a custom post type (a.k.a. CPT). Once they are defined as a CPT, you then associate wach employee with a company.

As @swisspedy mentioned in his comment, one way to do this is with the Posts 2 Posts plugin. While a fan of Posts 2 Posts myself, the fact that it requires editing code to define these relationships may not be desired. The Types plugin enables you to manage CPT relationships from the Dashboard, but it's featureset leaves something to be desired.

Once you set up the employee CPT (in the code, via the Types plugin or some other method), you need to define the relationship between the business and employee CPTs. I have found Advanced Custom Fields to have a robust features and a very user-friendly interface. It will not only help you define the relationship, but it will let you customize how/where the employee selector appears on the 'Edit Business' screen. (On the other hand, the Types plugin lets you create Employees on-the-fly from the 'Edit Business' screen, just by entering the employee name).

Here is an example of code you would place in functions.php to accomplish this with the Posts 2 Posts plugin:

// Create business/employee relationship
add_action( 'p2p_init', 'register_p2p_connections' );
function register_p2p_connections(){
    p2p_register_connection_type(
        array(
             'name'           => 'business_to_employee',
             'from'           => 'business',
             'to'             => 'employee',
             'title'          => array(
                 'from' => 'Employees',
                 'to'   => 'Employer'
             ),
             'admin_column'   => 'to',
             'admin_dropdown' => 'to',
             'fields'         => array(
                 'title' => array(
                     'title'   => 'Position',
                     'default' => ''
    ))));
}

// Auto-publish posts created via P2P box are drafts (they default as drafts).
add_filter( 'p2p_new_post_args', 'p2p_published_by_default', 10, 2 );
function p2p_published_by_default( $args, $ctype ) {
    $args['post_status'] = 'publish';
    return $args;
}

To list the employees when viewing a business post, in the single-business.php template, you add some code like this:

// Find connected employee
$connected = new WP_Query( array(
  'connected_type' => 'business_to_employee',
  'connected_items' => get_queried_object(),
  'nopaging' => true,
) );
// Display connected employees
if ( $connected->have_posts() ) : ?>
    <h3>Employees:</h3>
    <ul>
    <?php while ( $connected->have_posts() ) : $connected->the_post(); ?>
        <li><?php 
        the_title();
        $position = p2p_get_meta( $post->p2p_id, 'position', true ); 
        echo ( empty($position) ? '' : ", $position" );
        ?></li>
    <?php endwhile; ?>
    </ul><?php 
endif;
// Restore original (business) post data
wp_reset_postdata();
share|improve this answer
    
Making them into custom post types definitely came out to be the best answer, then using that Posts 2 Posts plugin to associate them. I've looked into that plugin's code and yes needs some decent editing. I now need to figure out how to show associated post types in the admin editor page... –  Aaron Aug 14 '13 at 19:49
    
@Aaron some of my code from a past project might help you. I've updated my answer to include it. –  smhmic Aug 15 '13 at 14:19

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.