我关于交换的第一个问题。我希望我能尽可能清楚地说明这一点,因为我不知道所有正确的术语。我想做的是建立一个staff表。易于理解的但是,我需要导演出现在第一个单元格中,其他人按姓氏的字母顺序出现。这是我正在为SiteOrigin页面生成器构建的人员网格小部件,如果这样有帮助的话。我还使用了带有“director”复选框的ACF。下面的代码是我到目前为止的代码。提前感谢您的帮助。
目前的结果是:我拿到了表格,主任是第一位,但工作人员不是按字母顺序排列的
预期结果:主管“m”|职员“a”|职员“b”|职员“c”|……|职员“m”|职员“n”|。。。
功能。php
// List staff member by last name
function posts_orderby_lastname ($orderby_statement, $query) {
global $wpdb;
//Used var_dump to view meta data to check for
$query = get_post_meta(get_the_ID(), \'director\', true);
if ($query == \'\') {
$orderby_statement = "RIGHT(post_title, LOCATE(\' \', REVERSE(post_title)) - 1) ASC";
} else {
$orderby_statement = ""
}
return $orderby_statement;
}
员工网格。php
add_filter( \'posts_orderby\' , \'posts_orderby_lastname\', 10, 2 );
//Query all staff taxonomy posts
$staff_args = array(
\'post_type\' => \'staff\',
\'staff-category\' => $the_categories,
\'meta_query\' => array(
\'relation\' => \'OR\',
array(
\'key\' => \'director\',
\'value\' => \'yes\',
\'compare\' => \'!=\',
),
array(
\'key\' => \'director\',
\'value\' => \'\',
\'compare\' => \'NOT EXISTS\',
),
),
\'orderby\' => \'meta_value\',
\'order\' => \'DESC\',
);
$query = new WP_Query( $staff_args );
// Begin iteration
$i = 0;
if ($query->have_posts()):
<table>
<tr>
while($query->have_posts()) : $query->the_post();
<td> $post </td>
$i++;
if($i % 2 == 0) {
echo \'</tr>\';
}
endwhile;
echo \'</table>\';
remove_filter( \'posts_orderby\' , \'posts_orderby_lastname\' );
endif;
wp_reset_query();
最合适的回答,由SO网友:Tori Booker 整理而成
这可能不是最好的方法,但我能够达到我想要的结果。我最后设置了第二个查询。
功能。php
// List staff member by last name
function posts_orderby_lastname ($orderby_statement) {
$orderby_statement = "RIGHT(post_title, LOCATE(\' \', REVERSE(post_title)) - 1) ASC";
return $orderby_statement;
}
员工网格。php
echo \'<table>\';
echo \'<tr>\';
// Query for Directors
$meta_query = (array(
\'post_type\' => \'staff\',
\'staff-category\' => $the_categories,
\'meta_query\' => array(
array(
\'key\' => \'director\',
\'value\' => \'yes\',
\'compare\' => \'!=\'
),
),
\'orderby\' => \'meta_value\',
\'order\' => \'DESC\'
));
$meta = new WP_Query( $meta_query );
// Begin iteration
$i = 0;
if($meta->have_posts()) :
while($meta->have_posts()) : $meta->the_post();
$table_cell = \'<td>$posts</td>\';
echo $table_cell;
$i++;
if($i % 2 == 0) { echo \'</tr>\'; }
endwhile; $meta->reset_postdata();
endif;
add_filter( \'posts_orderby\' , \'posts_orderby_lastname\', 10, 2 );
//Query all staff posts that are not directors
query_posts (array(
\'post_type\' => \'staff\',
\'staff-category\' => $the_categories,
\'meta_query\' => array(
array(
\'key\' => \'director\',
\'compare\' => \'NOT EXISTS\'
),
),
));
if(have_posts()) :
while(have_posts()) :
the_post();
$table_cell = \'<td>$posts</td>\';
echo $table_cell;
$i++;
if($i % 2 == 0) { echo \'</tr>\'; }
endwhile;
remove_filter( \'posts_orderby\' , \'posts_orderby_lastname\' );
$i++;
if($i % 2 == 0) { echo \'</tr>\'; }
endif;
echo \'</table>\';
wp_reset_query(); //Final reset just in case anything was missed