Support has been upgraded!
The Support Forum is closed. Not to worry! Providing the top quality support you expect and we're known for will continue! We're not ending support, just changing where you submit requests. This will provide you with the best experience possible.
Premium Support
Have you purchased an addon for Connections such as one of our premium templates or extensions with a valid license and you need help?
Please open a Support Ticket in your user account.
Free Support
Are you using the free Connections plugin? Don't worry, you are still very important to us! We are still providing you with the same high quality support that we're known for.
Please open a new support topic in the WordPress support forums for Connections.
Tagged: 8.5.25, meta_query, shortcode
- This topic has 5 replies, 2 voices, and was last updated 9 years, 10 months ago by
Steven Zahm.
-
AuthorPosts
-
09/09/2016 at 3:47 pm #390590
Stephen Garratt
ParticipantIs it possible to filter results based on a custom field?
09/12/2016 at 11:14 am #390831Steven Zahm
Keymaster@ Stephen
Yes, but only by using the shortcode
meta_queryshortcode option. I’ve not written the docs for this yet. Here’s one working example I gave to another user.http://connections-pro.com/support/topic/filter-listings-by-certain-field/#post-379891
And more complex example:
meta_query='{"meta_query":{"relation":"AND","0":{"key":"color","value":("red","orange"),"compare":"IN"},"1":{"key":"featured","value":"1","compare":"="}}}'Both of these are fairly complex examples… depending on hat you are looking to do, the
meta_querycan be much simpler.Hope that helps, let me know.
09/16/2016 at 3:19 pm #391790Stephen Garratt
ParticipantHi Steve,
Thanks for your help. I tried the code:
[connections meta_query='{"meta_query":{"0":{"key":"field_id_membership-cat","value":"supervisor","compare":"!="}}}']and it displays all the records that have some value in the custom field, but it doesn’t filter them depending on the value.
I am not a coder and and kind learning as I go along. Below is the snippet I used to create my custom field. I want to have a directory page that only displays entries with the value “Analyst” and a separate page that only displays entries with the value “Supervisor”
// Register the metabox and fields. add_action( 'cn_metabox', 'cn_register_custom_membership_category' ); function cn_register_custom_membership_category() { $atts = array( 'title' => 'Your AJA Membership Category', 'id' => 'membership-cat', 'context' => 'normal', 'priority' => 'core', 'fields' => array( array( 'name' => 'Field Name', 'show_label' => FALSE, 'id' => 'field_id_membership-cat', 'type' => 'checkboxgroup', 'options' => array( 'Candidate' => 'Candidate', 'Analyst' => 'Analyst', 'Supervisor' => 'Supervisor', 'Professional Member' => 'Professional Member', ), 'default' => '', ), ), ); cnMetaboxAPI::add( $atts ); }09/16/2016 at 4:26 pm #391794Steven Zahm
Keymaster@ Stephen
yeah… that’s not going to work.
The checkboxgroup field type is saved as a JSON encoded array in the database. Basically this means unless none of the options were checked the field saved in a way that is not possible to use meta_query. When no options are checked, nothing is saved except an empty value for the meta key.
The meta query you’re using will not work because it is basically selecting all entries which have the
field_id_membership-catID that doesnot equalthe value ofsupervisor. So the result would be all entries where you selected any of the options in the checkbox group because all entries where you have selected more than one option will always not be equal to “superviser”.To make this work…
First, you need to register each option in your current checkboxgroup as an individual checkbox. They can all be added to the same metabox. You would just add the four checkboxes to the same
fieldsarray.The meta query could also be simpler … I think. Honestly, I use it so infrequent that it takes me few attempts to get it right. But I think this is what you would use.
`meta_query='{“meta_key”:”field_id-supervisor”,”meta_value”:”1″}’
The above meta query assumes a checkbox field with the field ID of
field_id-supervisorand will display all entries with that checkbox selected.Hope that helps!
ps. I do offer custom development if you find this not in your “wheelhouse”. If you are interested in this, please contact me using the Contact link at the very bottom of the page for a quote.
10/08/2016 at 5:38 pm #394759Stephen Garratt
ParticipantHi Steve,
Will this new function allow me to Filter on Custom Field?
NEW: Introduce the cn_search_like_shortword filter.
Stephen
10/10/2016 at 5:39 pm #394961Steven Zahm
Keymaster@ Stephen
No, you still need to use the meta query. This filter allows other plugins to change the default query behavior for short word queries. Short words are words less or equal two characters. Databases generally can not search for small words so they have to be dealt with specially. Connections defaults to search for words that end with the short word. Another user wanted to change this so it would search for results that contain the short word. This filter allows that user to write code to change it to work how they want.
Hope that explains it clearly!
-
AuthorPosts
You cannot reply to this support topic. Please open your own support topic.
