How to associate more than one user to a post?

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).

Read More

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?

Related posts

1 comment

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

Comments are closed.