Jump to content

Order post query by custom field value (repeater field)


cerstrand_mace

Recommended Posts

I want to order my query by one of my custom field values. That custom field value is a repeater field inside a regular custom field. However, the problem is that my query is set to only show pages from a certain template, wich is done by the meta_key and meta_value attributes. As far as I get it the meta_key and meta_value should also be used to order my query. How can I do both? My query looks like this:

query_posts(array( 		'meta_key' => '_wp_page_template',		'meta_value' => 'template-kursny.php',		'showposts' => -1,		'post_type' => 'page', 		));

I've tried to google this for a while now and I've found similar cases but haven't found a working solution yet.

Link to comment
Share on other sites

You want to use those values to sort the data? Does the query_posts function have a way to specify sort criteria? If not, if it returns an array of posts, then you can use a custom sort function to sort the array.

Link to comment
Share on other sites

You want to use those values to sort the data? Does the query_posts function have a way to specify sort criteria? If not, if it returns an array of posts, then you can use a custom sort function to sort the array.
How would such a custom sort function look like? The values I wan't to sort this array by are the ones I have in a repeater-field, a date to be specific. The subfield name is "slutdatum".
Link to comment
Share on other sites

You use usort, uasort, or uksort to sort an array using a user-defined function: http://www.php.net/manual/en/function.usort.php You just need to write a function that takes 2 values, which would be 2 elements in your array, and you return a value less than or greater than 0 to indicate which element should come first in the array. If it returns 0 then that means the elements are equal. There are a few examples on that manual page, and several comments at the bottom.

Link to comment
Share on other sites

  • 3 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...