• I am creating a client portal were my clients can maintain their projects and leave feedback. 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 seem to be having trouble with the value and the compare. When I change the value to the clients actually ID number I get all the assign projects for client A, but when viewing client B I am viewing client A projects ( I know this because I am assigning the value to a specific client ID). Now when I change the value to get_the_ID() it does not display any of the post unless I change the compare to NOT IN. Is there a way I can display all selected project post to its assign client without displaying all post I have created in the custom project post?

    Here is my code for single.php which will be displaying the projects on the clients portal.

    <?php
     global $post;
    $projects = new WP_Query( array(
    'post_type'         => array( 'projects'),
    'posts_per_page'    => -1,
    'post_status'     => 'publish',
        'meta_query'        => array(
            array(
                'key'           => 'clientType',
                'value'         => array( $postid = get_post_meta( $post->ID, 'clientType', true )),
                'compare'       => 'LIKE',
                'type'          => 'NUMERIC'
    ))));
     ?>
    <?php while ( $projects->have_posts() ) : $projects->the_post(); ?>

    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.

    class projects {
       var $FOR_POST_TYPE = 'projects';
       var $SELECT_POST_TYPE = 'client_portal';
       var $SELECT_POST_LABEL = 'Client';
       var $box_id;
       var $box_label;
       var $field_id;
       var $field_label;
       var $field_name;
       var $meta_key;
        function __construct() {
          add_action( 'admin_init', array( $this, 'admin_init' ) );
        }
     function admin_init() {
         add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
         add_action( 'save_post', array( $this, 'save_post' ), 10, 2 );
        $this->meta_key     = "_selected_{$this->SELECT_POST_TYPE}";
        $this->box_id       = "select-{$this->SELECT_POST_TYPE}-metabox";
        $this->field_id     = "selected-{$this->SELECT_POST_TYPE}";
        $this->field_name   = "selected_{$this->SELECT_POST_TYPE}";
        $this->box_label    = __( "Select {$this->SELECT_POST_LABEL}", 'projects'         );
        $this->field_label  = __( "Choose {$this->SELECT_POST_LABEL}", 'projects'    );
       }
     function add_meta_boxes() {
      add_meta_box(
           $this->box_id,
           $this->box_label,
           array( $this, 'select_box' ),
           $this->FOR_POST_TYPE,
           'side'
        );
     }
       function select_box( $post ) {
         $selected_post_id = get_post_meta( $post->ID, 'clientType', true );
         global $wp_post_types;
         $save_hierarchical = $wp_post_types[$this->SELECT_POST_TYPE]->hierarchical;
         $wp_post_types[$this->SELECT_POST_TYPE]->hierarchical = true;
         wp_dropdown_pages( array(
          'id' => $this->field_id,
          'name' => $this->field_name,
         'selected' => empty( $selected_post_id ) ? 0 : $selected_post_id,
         'post_type' => $this->SELECT_POST_TYPE,
         'show_option_none' => $this->field_label,
              ));
         $wp_post_types[$this->SELECT_POST_TYPE]->hierarchical = $save_hierarchical;
              }
         function save_post( $post_id, $post ) {
            if ( $post->post_type == $this->FOR_POST_TYPE && isset( $_POST[$this->field_name] ) ) {
          update_post_meta( $post_id, 'clientType', $_POST[$this->field_name] );
           }
         }
       }
     new projects();
  • The topic ‘Display a query with multiple post types and same relationship on a single page’ is closed to new replies.