我不确定这是否可能(也张贴在ACF论坛上https://support.advancedcustomfields.com/forums/topic/ordering-by-custom-field-of-relationship-field-object/#post-62454)
我有两种自定义的帖子类型,Artworks和Artisters。Artworks有一个ACF post object字段“Artisters\\u name”,将其与单个艺术家关联。艺术家有自定义字段“Artister\\u last\\u name”和“Artister\\u first\\u name”。在Artworks archive页面上,帖子是按meta\\u键“Artisters\\u name”的meta\\u值排序的,当然它是按帖子ID排序的。
我正试图通过相关艺术家的姓氏然后是名字来查询orderby。(即,我想使用Post Type B中的meta值对Post Type A进行排序。)
到目前为止,我有一个查询,它按预期工作:
$args = array(
\'post_type\' => \'artworks\',
\'posts_per_page\' => -1,
\'meta_key\' => \'artists_name\',
\'orderby\' => \'meta_value\'
);
然后我想找一个
posts_join
筛选以查找post meta,其post ID=“Artisters\\u name”的meta\\u值
add_filter(\'posts_join\', \'trial_join\', 10, 2 );
function trial_join($joins) {
if(is_post_type_archive(\'artworks\') || (is_admin() && $_GET[\'post_type\'] == \'artworks\')) {
global $wpdb;
$joins .= " INNER JOIN {$wpdb->postmeta} artists_name ON artists_name.post_id={$wpdb->posts}.ID WHERE artists_name.meta_key=\'artist_last_name\'" ;
$joins .= " INNER JOIN {$wpdb->postmeta} first_name ON first_name.post_id={$wpdb->posts}.ID WHERE (first_name.meta_key=\'artist_first_name\' AND first_name.post_ID=artists_name.meta_value)";
$joins .= " INNER JOIN {$wpdb->postmeta} last_name ON last_name.post_id={$wpdb->posts}.ID WHERE (last_name.meta_key=\'artist_last_name\' AND last_name.post_ID=artists_name.meta_value)";
}
return $joins;
}
然后使用
posts_orderby
过滤器:
add_filter(\'posts_orderby\', \'orderby_pages_callback\', 10, 2);
function orderby_pages_callback($orderby_statement, $wp_query) {
$orderby_statement = "last_name.meta_value first_name.meta_value ASC";
return $orderby_statement;
}
但它不起作用。我已经拼凑了一些其他帖子的链接。有什么建议吗?
SO网友:drm10014
这是可行的。过滤器绑定到自定义\'orderby\'
并在查询后删除。
$args = array(
\'post_type\' => \'artworks\',
\'posts_per_page\' => -1,
\'no_found_rows\' => true,
\'order\' => \'ASC\',
\'orderby\' => \'artist_name_artwork_title\',
);
add_filter(\'posts_join\', \'name_join\', 10, 2 );
add_filter(\'posts_orderby\', \'orderby_artist_name_artwork_title\', 10, 2);
// query
$wp_query = new WP_Query( $args );
remove_filter(\'posts_join\', \'name_join\', 10 );
remove_filter(\'posts_orderby\', \'orderby_artist_name_artwork_title\', 10 );
function name_join($joins, $wp_query) {
if ( $wp_query->get( \'orderby\' ) != \'artist_name_artwork_title\' ) {
return $joins;
}
global $wpdb;
$joins .= " LEFT JOIN $wpdb->postmeta name ON name.post_id=$wpdb->posts.ID AND name.meta_key=\'artists_name\'" ;
$joins .= " LEFT JOIN $wpdb->postmeta lastname ON lastname.post_id=name.meta_value AND lastname.meta_key=\'artist_last_name\'";
$joins .= " LEFT JOIN $wpdb->postmeta firstname ON firstname.post_id=name.meta_value AND firstname.meta_key=\'artist_first_name\'";
return $joins;
}
function orderby_artist_name_artwork_title($orderby_statement, $wp_query) {
if ( $wp_query->get( \'orderby\' ) != \'artist_name_artwork_title\' ) {
return $orderby_statement;
}
global $wpdb;
$orderby_statement = "lastname.meta_value, firstname.meta_value, $wpdb->posts.post_title";
return $orderby_statement;
}