TODO: To change the header's content go to Dr.Explain menu Options : Project Settings : HTML (CHM) Export : Setup HTML Template and Layout
×
Menu
Index

Xắp xếp

- Xắp xếp kết quả trả về theo meta key hay custom fields.
// order by custom field with numeric
$args=array(
    'post_type'=>'post',
    'meta_key'=>'field1',
    'orderby'=>'meta_value_num',         #theo numeric
    //'orderby'=>'rand'       #random
    'order'=>'desc'
);
 
- Xắp xếp nhiều giá trị custom fields.
function orderbyreplace($orderby) {
    return str_replace('menu_order', 'mt1.meta_value, mt2.meta_value', $orderby);
}
$args = array(
  'post_type'=>'Events',
  'orderby' => 'menu_order',
  'order' => 'ASC',
  'meta_query' => array(
        array(
            'key' => 'Start_Hour',
            'value' => '',
            'compare' => 'LIKE'
        ),
        array(
            'key' => 'Start_Minute',
            'value' => '',
            'compare' => 'LIKE'
        )
    )
);
 
add_filter('posts_orderby','orderbyreplace');
$loop = new WP_Query( $args );
remove_filter('posts_orderby','orderbyreplace');
 
Trong phiên bản wordpress cao hơn, thêm tiền tố 'wp_posts' vào menu_order, nếu như đoạn code ở trên không hoạt động.
return str_replace('wp_posts.menu_order', 'mt2.meta_value, mt1.meta_value', $orderby);
 
 
Made with help of Dr.Explain

Unregistered version