Viewing 5 reply threads
  • Author
    Posts
    • #7678

      I scoured the forums but couldn’t find anything definitive. Is it possible to relate a user to a post (without making them the author)?

      If so, how is it done? I couldn’t find any tutorial in the docs but saw a few things on it like this: https://piklist.com/support/topic/post-to-user-relationships/

      I tried :

      <?php
      /*
      Title: Post Relate
      Post Type: 
      */
      
      piklist('field', array(
          'type' => 'post-relate'
          ,'scope' => 'user'
          
          ,'template' => 'field'
        ));
        
      ?>

      but that doesn’t work. I also tried using user_meta in the scope but it’s not working. Any help is appreciated.

    • #7687
      Steve
      Keymaster

      Assuming this is in wp-admin, and when creating/editing a post. Try this (untested):

      piklist('field', array(
        'type' => 'group'
        ,'scope' => 'user'
        ,'label' => 'User'
        ,'relate' => array(
          'scope' => 'post'
        )
        ,'fields' => array(
          array(
            'type' => 'text'
            ,'label' => 'Login'
            ,'field' => 'user_login'
            ,'columns' => 6
          )
        )
      ));
      
      // Display related users
      $related = get_users(array(
        'order' => 'DESC'
        ,'user_belongs' => $post->ID
        ,'user_relate' => 'post'
      ));
      ?>
      
      <?php if ($related): ?>
      
        <h4><?php _e('Related Users', 'piklist-demo');?></h4>
      
        <ol>
          <?php foreach ($related as $related_user): ?>
            <li><?php _e($related_user->user_login); ?></li>
          <?php endforeach; ?>
       </ol>
      
      <?php endif; ?>
    • #7708

      Thanks, Steve and happy new year!

      This just seems to create an empty field on the view in wp-admin. I tried changing the type to checkbox but notta.

      Any suggestions or do you have any learning guides on the relate system? I can only seem to find things in the forum and only in bits and pieces.

    • #7726
      Steve
      Keymaster

      @friendlyfire3– you would enter the user login into that text field.

      If you want to display a dropdown of users to choose from, this tutorial should help >

    • #7795

      Hey Steve, sorry to drudge up this post but took some time off and just getting back to this.

      How would I go about getting a list of posts related to the user on the frontend?

      I’ve tried this (some code excluded for brevity):

        
      
          $user_id = get_current_user_id();
          $key = '__relate_post';
          $single = false;
      
        $user_posts = get_user_meta($user_id, $key, $single );
      
      $related_proposals = get_posts(array(
            'post_type' => 'proposal' // Set post type you are relating to.
            ,'posts_per_page' => -1
            ,'post_belongs' => $user_id
            ,'post_status' => 'publish'
            ,'suppress_filters' => false // This must be set to false
        ));
        
      
        $output = '';
      
        if (!empty($related_proposals) ) { 
       
            $output .= '<ul>';
      
            foreach($related_proposals as $related_proposal) {
              $output .= '<li><a href="'.get_post_permalink($related_proposal->ID).'">'.$related_proposal->post_title.'</a></li>';
            }
            
            $output .= '</ul>';
        }
      
      return $output

      The above returns absolutely nothing.

      and then I tried this:

        
      
          $user_id = get_current_user_id();
          $key = '__relate_post';
          $single = false;
      
        $user_posts = get_user_meta($user_id, $key, $single );
      
      $related_proposals = get_posts(array(
            'post_type' => 'proposal' // Set post type you are relating to.
            ,'posts_per_page' => -1
            ,'post_belongs' => $user_posts
            ,'post_status' => 'publish'
            ,'suppress_filters' => false // This must be set to false
        ));
        
      
        $output = '';
      
        if (!empty($related_proposals) ) { 
       
            $output .= '<ul>';
      
            foreach($related_proposals as $related_proposal) {
              $output .= '<li><a href="'.get_post_permalink($related_proposal->ID).'">'.$related_proposal->post_title.'</a></li>';
            }
            
            $output .= '</ul>';
        }
      
      return $output

      This returns all posts for the type- not just the ones associated.

      And then tried this:

        
      
          $user_id = get_current_user_id();
          $key = '__relate_post';
          $single = false;
      
        $user_posts = get_user_meta($user_id, $key, $single );
      
      $related_proposals = get_posts(array(
            'post_type' => 'proposal' // Set post type you are relating to.
            ,'posts_per_page' => -1
            ,'include' => $user_posts
            ,'post_status' => 'publish'
            ,'suppress_filters' => false // This must be set to false
        ));
        
      
        $output = '';
      
        if (!empty($related_proposals) ) { 
       
            $output .= '<ul>';
      
            foreach($related_proposals as $related_proposal) {
              $output .= '<li><a href="'.get_post_permalink($related_proposal->ID).'">'.$related_proposal->post_title.'</a></li>';
            }
            
            $output .= '</ul>';
        }
      
      return $output

      and using “include” rather than post_belongs works but if there’s no ids in the array from the user meta, then all posts show up.

      None of these do what I intend. I just want to show only the posts that we’ve related to the user.

      You really need better docs on this relate stuff! 😉

    • #7846

      Bump. Any suggestions on this?

Viewing 5 reply threads
  • You must be logged in to reply to this topic.