In a shortcode I can limit wp_query
results by custom field values.
Example:
[my-shortcode meta_key=my-custom-field meta_value=100,200 meta_compare='IN']
And obviously it’s possible to use multiple custom fields in a wp_query
like WP_Query#Custom_Field_Parameters
But how can I use multiple custom fields in my shortcode? At the moment I do pass all the shortcode parameters with $atts
.
On of a few different solutions might be to use a JSON encoded format for the meta values. Note that this isn’t exactly user-friendly, but certainly would accomplish what you want.
You would of course need to generate the json encoded values first and ensure they are in the proper format. One way of doing that is just using PHP’s built in functions:
Example usage in the shortcode:
Which then you would parse out in your shortcode function, something like so:
Alternate Method
Another method would be to cause your shortcode to accept an arbitrary number of them, with matching numerical indexes, like so:
Then, in your shortcode function, “watch” for these values and glue them up yourself: