8

I am creating a client portal were my clients can maintain their projects and leave feedback in Wordpress. I have two custom post types called "Clients" and "Projects" and they each pull information from one another in the backend. When I create a client it automatically generates its postID to a drop down menu on the projects post type were I can assign a client to a project.

What I am trying to accomplish is by displaying all projects that are associated with the selected client on a single page on the front end. The single page will be the client portal, which is generated by the client post type.

I can't seems to display the associated post. Here is my code for single.php which will be displaying the projects on the clients portal.

 <?php 
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
      $loop = new WP_Query( array(
         'post_type'      => array( 'projects'),
         'posts_per_page' => -1,
         'paged'          => $paged,
         'meta_query' => array(
              array(
                 'key'     => 'a_client', // name of custom field
                 'value'   => '"' . get_the_ID() . '"', 
                 'compare' => 'LIKE'
             )
          )                 
     )); 
 ?> 

Below is the code I am using to assign a client to a projects in the admin panel of projects. This script displays any client that I created in the client page and display their names in a drop down menu.

 add_action( 'add_meta_boxes', 'add_clients_custom_metabox' );
    function add_clients_custom_metabox() {
 add_meta_box( 'custom-metabox', __( 'Clients' ), 'clients_custom_metabox', 'projects', 'side', 'high' );
 }

 function clients_custom_metabox($post) {
     global $post,$current_user;
     //remember the current $post object
        $real_post = $post;
     //get curent user info (we need the ID)
       get_currentuserinfo();
     //create nonce
       echo '<input type="hidden" name="clients_meta_box_nonce" value="',       wp_create_nonce(basename(__FILE__)), '" />';
     //get saved meta
       $selected = get_post_meta( $post->ID, 'a_clients', true );
    //create a query for all of the user clients posts
       $clients_query = new WP_Query();
       $clients_query->query(array(
           'post_type'      => 'client_portal',
           'posts_per_page' => -1,
           'author'         => $current_user->ID));
    if ($clients_query->have_posts()){
          echo '<select name="a_clients" id="a_clients">';
      //loop over all post and add them to the select dropdown
          echo '<option>Assign a client</option>';
          while ($clients_query->have_posts()){
     $clients_query->the_post();
          echo '<option value="'.$post->ID.'" ';
                if ( $post->ID == $selected){
          echo 'selected="selected"';
               }
          echo '>'.$post->post_title .'</option>';
               }
          echo '<select>';
               }
     //reset the query and the $post to its real value
        wp_reset_query();
        $post = $real_post;
             }
    //hook to save the post meta
          add_action( 'save_post', 'save_clients_custom_metabox' );
    // Process the custom metabox fields
        function save_clients_custom_metabox( $post_id ) {
            global $post;
   // verify nonce
    if (!wp_verify_nonce($_POST['clients_meta_box_nonce'], basename(__FILE__))) {
        return $post_id;
   }
  // check autosave
     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
     return $post_id;
  }
 // check permissions
 if ('events' == $_POST['post_type']) {
 if (!current_user_can('edit_page', $post_id)) {
return $post_id;
 }
} elseif (!current_user_can('edit_post', $post_id)) {
return $post_id;
 }
if( $_POST ) {
 $old = get_post_meta($post_id, 'a_clients', true);
 $new = $_POST['a_clients'];
if ($new && $new != $old){
 update_post_meta($post_id, 'a_clients', $new);
}
 }
  }

Full code for single.php http://pastebin.com/na7djwsq

Register Post Type I am using Project post type is called => projects Clients post type is called => client_portal

  • why is paged set to $client_ID? you're also setting posts_per_page to return all posts. – Milo Apr 18 '14 at 16:52
  • originally I had $client_ID as $paged but nothing seemed to be displaying with any scenario I was doing. @Milo – bigant841 Apr 18 '14 at 16:57
  • Originally a Stack Overflow Question - feel free to join us in chatroom – Howdy_McGee Apr 18 '14 at 17:01
  • It does look like client should be a_client and @bigant841 is saving the meta as an array, so it looks like this: [a_clients] => Array ([0] => 91) – Howdy_McGee Apr 18 '14 at 17:04
  • Naming a post type in plural is unusual. Are you sure the correct slug is projects? Not project? – tao Jan 25 '15 at 17:08
1

You're setting the post meta as a_clients, but the query is looking for a_client.

update_post_meta($post_id, 'a_clients', $new);

'key' => 'a_client'

Those need to be the same. Since updating the query means you won't have to go updating posts again, I suggest updating the key of the meta_query to a_clients.

|improve this answer|||||

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.