按相关帖子类型B的自定义字段对A类帖子进行排序

时间:2018-08-22 作者:drm10014

我不确定这是否可能(也张贴在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;
}
但它不起作用。我已经拼凑了一些其他帖子的链接。有什么建议吗?

1 个回复
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;
}

结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post

按相关帖子类型B的自定义字段对A类帖子进行排序 - 小码农CODE - 行之有效找到问题解决它

按相关帖子类型B的自定义字段对A类帖子进行排序

时间:2018-08-22 作者:drm10014

我不确定这是否可能(也张贴在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;
}
但它不起作用。我已经拼凑了一些其他帖子的链接。有什么建议吗?

1 个回复
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;
}

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post