Query/filter by custom field?
-
Is it possible to query/filter by a custom field?
I have added a custom checkbox group to my directory. I would like to display a list of connections that have a specific value selected. Or, alternatively, group the connections by each option in the checkbox group.
For example, consider the following checkbox group options:
Option A
Option B
Option CJohn Doe has Option A and Option B selected.
Jane Doe has Option B selected.
Robert Smith has Option C selected.Ultimately, what I am trying to display is the following:
Option A
– John DoeOption B
– Jane Doe
– John DoeOption C
– Robert SmithI’m happy to build the Option sections on my page and use multiple shortcodes for each section if necessary.
Thoughts?
-
You might be able to utilize the
meta_query[connections]shortcode option:Can you share the code snippet for your checkboxes so I can confirm?
Here is the snippet from my functions.php:
add_action( 'cn_metabox', 'cn_register_merit_badge_metabox' ); function cn_register_merit_badge_metabox() { $atts = array( 'title' => 'Merit Badge Counselor', 'id' => 'merit_badge_counselor', 'context' => 'normal', 'priority' => 'core', 'fields' => array( array( 'name' => 'Merit Badges', // Change this field name to something which applies to you project. 'show_label' => TRUE, // Whether or not to display the 'name'. Changing it to false will suppress the name. 'id' => 'merit_badges', // Change this so it is unique to you project. Each field id MUST be unique. 'type' => 'checkboxgroup', // This is the field type being added. 'options' => Merit_Badge::merit_badge_names(), 'default' => '', // This is the default selected option. Leave blank for none. ), ), ); cnMetaboxAPI::add( $atts ); }I have a static class Merit_Badge, that maintains a list of merit badges for our scout troop. The static method, returns an associative array of merit badge ids and names.
Thanks!
Ok, this should work, but you will have to make a slight change…
Checkbox groups are saved as entry meta as a serialized array where the array values are the option keys. In this case, the merit badge ids.
What you should do is change the field type checkbox:
Iterate thru the
Merit_Badge::merit_badge_names()array to build thefieldsarray as shown in the doc.Doing this will ensure that each meta merit badge id is stored separately per entry. Then you should be able to use the
meta_queryfollowing theORexample.I hope this helps; please let me know.
This ticket is being marked resolved due to inactivity. If you still need help with this, please do not hesitate to let me know.
The topic ‘Query/filter by custom field?’ is closed to new replies.