I am using this to query posts by custom_field and value:
$posts = get_posts_sharing_custom_field_value($custom_field, $custom_value);
This works fine if the output is a single variable but not if its an array.
Do you have any functionality like this built in?
No, functionality for arrays is not guaranteed with the
get_posts_sharing_custom_field_value()function because array fields represent some architectural problems (mmm… might make a good feature request)… technically, the array fields store a JSON string that represents an array, so you could look for posts containing the same array, but that’s probably not what you want. It’s a limitation of the CCTM’s architecture, unfortunately, but it was the lesser of two evils. If your’e curious, here’s some of the thought process behind it: http://code.google.com/p/wordpress-custom-content-type-manager/wiki/CustomFieldsDataStructure
If you need a query that will query “repeatable” fields, I’d recommend using Summarize Posts’ GetPostsQuery class (included with the CCTM). It’s gotten more and more flexible, so you can give it arguments to match your query. It’s not streamlined for non-devs, but it should be working (eep! not sure if my unit tests are up-to-date for this). E.g. in your template file:
$args = array(); // initialize the array $args['my_custom_field']['like'] = '"red"'; $Q = new GetPostsQuery(); $results = $Q->get_posts($args);
The trick here is if you supply it with a 2nd layer of the array, you can define your own operator AND notice the double-quotes around the value you’re searching for.
To clarify, this would search for all custom fields where the value was EQUAL to red:
$args['my_custom_field'] = 'red';
Or more verbosely:
$args['my_custom_field']['='] = 'red';
But if you want to search more flexibly, you can use other operators:
$args['my_custom_field']['starts_with'] = 'red';
The above might match custom fields that contained “red” or “redistribute” or “redo” etc.
But since the repeatable fields store data as JSON arrays, you have to remember that the values are actually stored like something like this:
["red"] OR ["red","green","blue"]
So to search for those values, you first must use a different operator (“like” instead of “=”), AND you should include double-quotes in your search term. You can omit the latter step, but then you could get false-positives because searching for fields whose value is LIKE ‘%red%’ would match “bored”, “Bredy” etc.
Hope that makes sense. GetPostsQuery is so flexible it requires a lot more docs.
Thanks for the reply and being active in your forum. I’ll try that.
Here is exactly what I’m trying to do in case you have a better solution in mind.
I’m doing a site for my buddy who’s a tattoo artist and I was using custom fields like: tattoo_placement = [‘neck’, ‘shoulders’, ‘sleeves’] and wanted the user to be able to click “Sleeves” and see all post_type=tattoo with tattoo_placement=sleeves.
The functionality is working with custom_fields that have a single value like a text input but when it came to an array of values I ran into an issue where the query wasn’t working. I’ll try this and report back . Thanks again
Yeah, so you should be able to do something like this:
$args = array(); // initialize the array $args['post_type'] = 'tattoo'; $args['tattoo_placement']['like'] = '"sleeves"'; $Q = new GetPostsQuery(); $results = $Q->get_posts($args);
If you wanted to automate this, you could put read the placement out of a form submission or for a more static solution, you might have X dedicated pages where each page used a dedicated template file where you had hard-coded the search parameters.
I’m getting the value from a query string parameter so this would work perfect.
Thanks a lot!
I didn’t see this in the documentation. Sorry for bugging you here man
I didn’t see this in the documentation.
I should do more unit-testing on it first, but hopefully it’s working well enough to do what you need.
You are awesome man. That worked perfectly. I appreciate your help in here.
Awesome functionality. I was able to create a helper function that output linked custom values links that led to a custom query template page. Then using your code above the custom query grabbed the field and value from the query string and used it in a loop to show all posts with that field/value combo.
I’ll mark as resolved.
- The topic ‘Query Custom Value when output is an Array’ is closed to new replies.