Search code examples
phpwordpresspodscms

WordPress - Only Display Posts That Are Associated To Logged In User


In my WordPress Site, I have custom post types setup for Tanks, Locations, and Users using the PODS plugin. Currently I have some PHP that allows me to only show the logged in user a post, if they are listed as the author of it. Which is a good start, but not exactly how I need it to work. Because of the way WordPress stores their Author field, I am only able to have one User associated per Tank/Location instead of many. My question is, how would I go about making it so that WordPress will check to see what Tanks/Locations the current logged in user has associated to them, and only display those? In both the Tanks and Locations PODS, I have already setup a relationship field to my Users POD which allows me to assign each Tank/Location with as many users as necessary. All I need now is to figure out how display that information. If it makes any difference, the storage type for all my PODS is Table Based.


Solution

  • So in case anyone has the same question I did, here is how I ended up solving my issue. The variable 'users.ID' is referring to the relationship field I had in each of my Locations/Tanks.

    if(!current_user_can('administrator')){
    add_shortcode( 'pods_by_current_user_cpt', 'pods_by_current_user_cpt' );
    
        function pods_by_current_user_cpt( $atts, $content = null ) {
            $current_user  = wp_get_current_user();
            $user_id       = $current_user->ID;
            $atts['where'] = 'users.ID = ' . (int) $user_id;
            return pods_shortcode( $atts, $content );
        }
    }
    else{
        add_shortcode( 'pods_by_current_user_cpt', 'pods_by_current_user_cpt' );
    
        function pods_by_current_user_cpt( $atts, $content = null ) {
            $current_user  = wp_get_current_user();
            $user_id       = $current_user->ID;
            return pods_shortcode( $atts, $content );
        }
    }