Jump to content

Order post query by custom field value (repeater field)


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 post
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 post
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 post
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 post
Share on other sites
  • 3 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...