I’m trying to list all the posts with a taxonomy called ”priority” in a particular order in a category page.
1.) All posts that has checked ”gold” should come first in alphabetic order.
2.) Posts that has checked ”silver” comes right after (in alphabetic order).
When there is more than, let's say 5 posts in the whole list, there should be a paging function.
Any ideas?
My live test site:
[[LINK href="http://www.u7322036.fsdata.se/advokat/category/new-york/"]]http://www.u7322036.fsdata.se/advokat/category/new-york/[[/LINK]]
The attached image shows how it should work.
timDesain Nanang answers:
I think, there is no way to do this with native WP_Query
[[LINK href="http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters"]]http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters[[/LINK]]
You should change priority as meta filed instead of taxonomy:
[[LINK href="http://www.wpquestions.com/question/showChronoLoggedIn/id/9599"]]http://www.wpquestions.com/question/showChronoLoggedIn/id/9599[[/LINK]]
hlx5 comments:
Interesting approach, do you have a suggestion how the code should look like in this case?
The posts are regular posts.
timDesain Nanang comments:
try this code:
//priority
function wpq_priorities(){
return array(
9 => 'Platinum',
7 => 'Gold',
5 => 'Silver',
);
}
add_action('add_meta_boxes', 'wpq_meta_add_new');
function wpq_meta_add_new() {
add_meta_box('wpq_meta_priority', 'Priority', 'wpq_meta_priority_form', 'post', 'side', 'high' );
}
function wpq_meta_priority_form($post) {
wp_nonce_field('wpq_meta_priority_form', 'wpq_meta_priority_form_nonce');
$priority = (get_post_meta($post->ID, 'wpq_cf_priority', true));
foreach(wpq_priorities() as $key=>$value){
echo '<p><input type="radio" name="wpq_cf_priority" id="pr-'.$key.'" value="'.$key.'" '.($priority==$key ? 'checked="checked"' : '' ).'/> <label for="pr-'.$key.'">'.$value.'</label></p>';
}
}
add_action('save_post', 'wpq_meta_priority_action');
function wpq_meta_priority_action($post_id) {
if(!isset($_POST['wpq_meta_priority_form_nonce'])) return $post_id;
if(!wp_verify_nonce($_POST['wpq_meta_priority_form_nonce'], 'wpq_meta_priority_form')) return $post_id;
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
update_post_meta($post_id, 'wpq_cf_priority', $_POST['wpq_cf_priority']);
}
add_action( 'pre_get_posts', 'wpq_pre_priority' );
function wpq_pre_priority( $query ) {
if ( !is_admin() AND $query->is_main_query() ) { //you can add your condition here
$query->set( 'meta_key', 'wpq_cf_priority' );
$query->set( 'orderby', 'meta_value' );
$query->set( 'order', 'DESC' );
}
return;
}