I want to list some pod items on front end. To do this I am normally using pods
shortcode. To filter the data I use where
attribute inside pods shortcode. For example:
[pods name="salon" where="author.ID = '2'"]
<li>{@name}</li>
[/pods]
I want to filter pod items by the author.ID
. The user should see the pod items that are created by himself. It is possible to access the current user by wordpress api
global $current_user;
. Also it is possible to get it by using magic tags of Pods {@user.ID}
.
But the problem is I cannot use php code or magic tags inside shortcode such as this:
[pods name="salon" where="author.ID = '{@user.ID}'"]
I am looking for a way to overcome this problem.
I thought maybe I can write a new shortcode function where I can get the current user's id and then call the pods
shortcode function such as this:
function pods_by_current_user($atts) {
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
// put it into 'where' attribute and delegate to the "pods" function
Do you think that this is the right way to solve this problem? And do you know how to put $user_id
into $atts
and delegate the rest to the actual pods
shortcode function?
In your pods_by_current_user function, try doing:
$atts[ 'where' ] = 'author.ID = ' . (int) $user_id;
and then return the normal Pods shortcode:
return pods_shortcode( $atts );
This will let you use any of the normal [pods] shortcode attributes as usual, but will set the 'where' to automatically limit to the current user ID. I'm sure you can make this more complex, but that will do what you're after.