Forums › Forums › Search & Filter Pro › Filter content by current user
Tagged: current user, logged in, user submission
- This topic has 5 replies, 2 voices, and was last updated 7 years, 1 month ago by Trevor.
-
Anonymous(Private) October 5, 2017 at 11:08 am #134903
Hi Trevor,
is it possible to display only posts generated by the current user?
I set up a custom post type (user_notes) and a front end form to allow users to generate their own user notes. Is ist possible to add a filter to the user notes page that shows only content generated by the current user?
Couldn’t find anything like that in Post Meta.
Thanks a lot, regards
Georg
Trevor(Private) October 5, 2017 at 12:13 pm #134914IF it could be done, it would very very difficult. My guess is that you would have to use the Edit Query Arguments Filter.
The big issue would be security. How to stop someone spoofing the url query? Not something I could advise on.
Anonymous(Private) October 6, 2017 at 10:56 am #135093Hi Trevor,
thanks for pointing me to Edit Query Arguments Filter, must have missed that!
Here is how I set things up:
– a front end form for submitting private custom posts (cpt supports author)
– search & filter restricted to
– post type: my custom post type
– post status: privateI expected search & filter to take into account that limitation to private actually limits search results to current users posts, as a regular wordpress query would do. But maybe my setup is wrong.
If s&f doesn’t work this way, consider this a feature request. Because otherwise it renders wordpress post status private useless.
Next, I tried your approach of editing Arguments Filter, but it gives me no results. Probably wrong syntax?
function filter_user_posts( $query_args, $sfid ) { $user_id = get_current_user_id(); //if search form ID = 109, do something with this query if($sfid==109) { //modify $query_args here before returning it $query_args = array( 'author' => $user_id ); } return $query_args; } add_filter( 'sf_edit_query_args', 'filter_user_posts', 20, 2 );
Speaking of security issues: I will further investigate the URL-snooping issue. But I also have a content restrictions on single post view in other places, so posts can’t be viewed by non-authors anyway.
Cheers,
Georg
Anonymous(Private) October 19, 2017 at 10:21 pm #137453Hi Trevor,
a quick update, just in case someone is looking for a solution. The following filter works for me,
only posts written by currently logged-in user will be displayed.function filter_function_name( $query_args, $sfid ) { //if search form ID = 100, then do something with this query if($sfid==100) { //modify $query_args here before returning it $query_args = array( 'post_type' => 'my_custom_post_type', 'author' => $user->ID ); } return $query_args; } add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
Thanks,
Georg
-
AuthorPosts