“Where” parameter equal only content created by user
-
Hi,
I’m doing a little portal where each user adds its content, the issue is with relationships as you can see all content that other user created, so when you’re setting a relationship you get confused by others content and I would like to limit it to only related content that the user created.
I’m looking at the “where” parameter in the “Relationship options” and would like to add a “only content created by the user that is logged in / that created its content”
Do you have any recommendations?
Thank you!
-
I would need something like:
t.post_author = <?php get_the_author_meta( 'ID' ); ?>
or
t.post_author = get_current_user_id();
I’ve also tried with the special magic tags (single quotes removed to post here):
(t.post_author = {@user.id})
is there a way to run php to construct the query or are they any variables / magic tags that are expanded?
or maybe a hook to filter the results of the query ?
Like the option “Limit list by Post Status” but instead of the post status the user that created the content
Hi @allanext
It looks like you are mixing different coding structures together which of course cannot be done.
How are you working with Pods? Pod templates? Shortcodes? PHP?
Cheers, Jory
hi @keraweb
yes, those are just intentions .. and perhaps I’ve not explained well; it’s all about the back-end admin side of WordPress. I don’t want that users can see content created by other users, it’s a portal, so if I’m trying to connect an organization to its people (content type “person”), the relationship field (for example “volunteers” of type “person”) gets a dropdown filled with people that all organization have created, instead of only the ones the specific organization/user has created.
For example to show only the content that the user/organizations has created in the view list of organizations, people, locations and projects, I’m using a filter like this in functions.php:
// limit results on admin interface if user isn't the creator of the content function ddn_filter_results( $posts ) { $my_posts = array(); $content_types = array( "person", "organization", "project", "location" ); if (is_admin()) { foreach ( $posts as $post ) { if ( in_array($post->post_type, $content_types) ) { $user = wp_get_current_user(); global $current_user; wp_get_current_user() ; if ($post->post_type == "organization" && ($current_user->has_cap( 'read_others_organization' ) || $post->post_author == $current_user->ID) || $post->post_type == "person" && ($current_user->has_cap( 'read_others_person' ) || $post->post_author == $current_user->ID) || $post->post_type == "location" && ($current_user->has_cap( 'read_others_location' ) || $post->post_author == $current_user->ID) || $post->post_type == "project" && ($current_user->has_cap( 'read_others_project' ) || $post->post_author == $current_user->ID)) { $my_posts[] = $post; } } else { $my_posts[] = $post; } } } else { $my_posts = $posts; } return $my_posts; } add_filter( 'posts_results', 'ddn_filter_results', 10, 2 );
`
This limit’s on the backend of wordpress what a specific user can see to only the content that it has created.
To do this I’ve created some additional capabilities like “read_others_organization” and then assigned permissions.I need to filter on the backend the filled lists of dropdown (relationships) with only content (for example people) that a specific organization/user has created, a little bit like “Limit list by Post Status” where you filter the content by it’s status, but instead filter the dropdown list with only content created by the user logged in. If an organization is editing that field and see’s hundreds of people it won’t be very user friendly.
I could be done also with a capability but it didn’t seem to work with the option “Restrict access by Capability”. That’s why i was trying to add something in the “Customized WHERE” to limit to only content that is created by the user.id
Please let me know what would be a good aproach.
Thank you!Hi. I can only tell you what has been working for me. Do not know if there is a “better” way or not:
I am using code to create a shortcode [list_podxyz], and I put that code into an normal WP page. The code allows me to put a where statement before I loop thru the CPT podxyz, using a field I added called memberuserid. It is a number field and each record has the user id of the creator of each record.
What I do to populate that field is also using code to create a PODS form, and I default the value to whatever user id is logged in and doing the insert/edit of that specific record. I also set the memberuserid field to hidden, since they do not have to see or know this info.
It all seems to work well.
The reason I hesitate to say this is the “best” way or not, is the relationships thing. I was thinking that it might offer another method of attaching (in code) a submitted form, by some sort of relationship to the user, etc.
Good luck,
Jamiethe issue is with relationships as you can see all content that other user created, so when you’re setting a relationship you get confused by others content and I would like to limit it to only related content that the user created.
Hi @allanext,
The list of posts displayed in relationship fields can be filtered with the filter
pods_field_pick_data
. Its reference can be found here: https://docs.pods.io/code-snippets/multiple-fields-pick-list/There was a very similar request yesterday, related to filtering the content of a multi-select list: https://wordpress.org/support/topic/filtering-a-multi-selct-list/
The responses in the thread are examples of usage of the filter documented above.
Combined with the function you’ve written above, this should filter the relationship fields in the necessary way.
Hi @pdclark,
This is great! I’ve just tried something like:
function pods_relations_pick_data($data, $name, $value, $options, $pod, $id){ global $current_user; wp_get_current_user() ; // var_dump($name); // echo '<pre>' .var_export($pod, true) . '</pre>'; $filter_data = array(); if ($name != "pods_meta_enti_collegati") { foreach ($data as $dataid => $el) { if($dataid){ $p = get_post($dataid); if ( $current_user->ID == $p->post_author || current_user_can('administrator') ) { $filter_data[$dataid] = $el; } } } $data = $filter_data; } return $data; } add_filter('pods_field_pick_data', 'pods_relations_pick_data', 1, 6);
which filters correctly all the relationships apart from the field
pods_meta_enti_collegati
.Thanks again.
- The topic ‘“Where” parameter equal only content created by user’ is closed to new replies.