Skip Navigation

[Resolved] Creating a new plugin and need a custom field that's available in view filters

This support ticket is created 4 years, 7 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 6 replies, has 2 voices.

Last updated by donC-3 4 years, 7 months ago.

Assisted by: Beda.

Author
Posts
#1603537

I want to create a new plugin. This would add some meta fields for posts that are custom to the plugin. I want to make one of them accessible to the query filter on a view. What's the best way to do this?

My current thinking on the data is it will have a comma delimited list of user ids such as:

,12,16,54,

And I need the filter to pull up posts that have in this data the current viewing user id . So I was thinking of using a LIKE and having it compare this new metafield LIKE the current user id (assuming I can use a shortcode attribute to retrieve that).

Make sense and if so how do I get my new meta field to show up in the custom field drop down? Or is there a better way?

#1604071

You can add custom fields using the WordPress API https://wordpress.org/support/article/custom-fields/ (update_post_meta, get_post_meta)
Those can then immediately be used in Views Queries, as they are just native WordPress Fields.

Now, if you want to create some more fancy fields than just a single line, and maybe also want them added to Toolset > Custom Fields (even if of your custom plugin), I would suggest having a look at this custom implementation here hidden link, it uses some (internal, not publicly documented) hooks of Toolset to create some Fields. That could help you in your custom development process, however, it's not something we can support in this forum.

Now, how you store your data mainly will depend on how you want to display it and search it.
Remember that if your fields are not Types fields they can't be complex (select, etc) as they would not work in Views properly (only raw values would be returned)

You can use the View query filters to filter these fields like they where single fields, that will work
Also, you can display raw values using the toolset wpv-post-field shortcode, https://toolset.com/documentation/user-guides/views/views-shortcodes/#vf-153444
But this will not allow for a lot of "fancy" output, as Views could not guess any particular custom field structures, like select or so.

Please let me know if this helps planning ahead?

#1604221

I think that gives me what I need. We'll see.

#1604861
Annotation 2020-04-28 144700.png

Almost there, added the custom fields, created a view which then filters based on a shortcode parameter. I see when I add the view it lets me fill the parameter value on the right. How do I get it to pull dynamically? I can build a shortcode but when put the shortcode in the box it doesn't work.

If there's not a way to do that (that is the ideal option), then is there a way to autofill with the current post author?

#1604991

I think I figured out I had to register mycustomshortcode in the toolset settings. Sound correct?

#1605185

If you use (Custom) ShortCodes in HTML conditionals, COnditional Blocks, or else, you'd have to register them in Toolset > Settings > Front End Content > Third-party shortcode arguments, that's right.

Does it work as you intended now?
I'm happy the examples made could help as a reference.

#1605671

Yes, seems to be working now fine. Thank you very much