Skip Navigation

[Resolved] Passing current author as shortcode attribute

This thread is resolved. Here is a description of the problem and solution.

Problem:

The issue here is that the user had their view filter to get the value from a shortcode attribute, but wanted to pass the current author into that shortcode attribute.

Solution:

This can be done by doing the following.

[wpv-view name="sales-dashboard-dealer" salesrep="[wpv-current-user info='id']"]

Where the [wpv-current-user info='id'] will get the ID of the current user.

This support ticket is created 5 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 8 replies, has 3 voices.

Last updated by kelvinL-2 5 years, 11 months ago.

Assisted by: Shane.

Author
Posts
#1221332
Screenshot 2019-03-27 at 1.29.33 PM.png
Screenshot 2019-03-27 at 1.32.34 PM.png

Dear Sir/Madam,

I have a View to filter all dealer post according to the field 'dealer-sales' equal to the shortcode attribute "salesrep", may I know how I can pass the current author as the shortcode attribute in the Views

#1221390

Dear Sir/Madam,

I temporary write this code to show the result without setting the Query Filter

		<wpv-loop>
          [wpv-conditional if="( $(wpcf-dealer-sales) eq '[wpv-user field="user_login"]' )"]
          <b>[wpv-post-title]</b><br/>
          地址:[types field='address'][/types]<br/>
          電話:[types field='tel'][/types]
          <hr/>
          [/wpv-conditional]
		</wpv-loop>

But I am sure this is not the best solution as it uses up a lot of resource for the query. Please kindly advise a better solution.

Best regards,

Kelvin.

#1221410

You can set a Query to show posts by author equal to (and here you have many options, one of them, by shortcode attribute)

That, once chosen, allows you to insert the view to a page and pass the current user ID as ShortCode attribute, for example.

But, you cannot compare a field to a shortcode attribute in a Query.
For that, HTML condition is the correct way to go as you do now.

You could, of course, set a Query filter to show posts by an author as set in your custom field (hence, again by shortcode attribute) and then insert that field in the View shortcode attribute for the author.
https://toolset.com/documentation/user-guides/passing-arguments-to-views/

#1221417

Dear Beda,

For my case, the owner of the dealer post is the user (who has the dealer role) but not the user with sales role. So I add a field to the dealer post and record the relationship between the dealer post and the sales role.

In WP5.1.1, I don't know how to pass the attribute to the View Shortcode like

[wpv-view name="sales-dashboard-dealer" author="[current_login_user]"]

in this new UI.

where the [current_login_user] is the custom shortcode or standard WP or Toolset shortcode if have.

Can I do like this to minimize the query resource?

Best regards,

Kelvin

#1221758

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Kelvin,

You can do it like this but with the shortcode [current_login_user] you will be passing the information of the current user into the view.

We actually have a shortcode like this.
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-current-user

Please let me know if this helps.

Thanks,
Shane

#1221759

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

If i've misinterpreted please let me know as well as provide a little more information so I know exactly the requirements.

#1221892
1221332-Screenshot_2019_03_27_at_1.32.34_PM.png

Dear Shane,

I want to add View shortcode in page and pass the current login user_id as the attribute to the [wpv-view name.....]

In the View, I pass the attribute value into the query filter.

Can I do that?

Kelvin

#1221977

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Kelvin,

Thank you for the clarity.

For your case you can just try this

[wpv-view name="sales-dashboard-dealer" salesrep="[wpv-current-user info='id']"]

This should provide the correct filtering information for your view filter.

Thanks,
Shane

#1223333

My issue is resolved now. Thank you!