sorting posts by multiple custom fields with wp_search
-
This topic has been raised many times and I have spent hours reading relevant codex reference and forum posts. The claimed solutions appear to rely upon adding a filter. The codex class reference for wp_search is extensive.The ‘orderby’ parameter is described as a string and multiple terms separated by space are allowed, as in:
'orderby' => 'title menu_order'
Custom fields are allowed as terms and an example including one custom fiels is given, but an example of the use of orderby multiple custom fields is not given. The Plugin API/Filter reference appears to be incomplete and no example gives any guide that helps in creating a posts_orderby filter using multiple custom fields.
My objective seemed simple. There is a custom post with custom fields:- last_name
- first_name
The complete code that orders posts in last_name sequence is:
<?php $args = array( 'post_type' => 'will', 'post_status' => 'publish', 'nopaging' => 'true', 'meta_key' => 'last_name', 'orderby' => 'meta_value', 'order' => 'ASC' ); $query = new WP_Query($args); while($query->have_posts()){ $query->next_post(); $id = $query->post->ID; $title = get_the_title($id); ?> <p><a href="?p=<?php echo $id ?>"><?php echo $id . ": " . $title ;?></a></p> <?php }This works fine.
I cannot, however, see how to extend this, even using meta_query, to sort in last_name, first_name sequence.
Also I do not see how to write a post_orderby filter to achieve the objective.
The topic ‘sorting posts by multiple custom fields with wp_search’ is closed to new replies.