WordPress自定义文章类型按标题排序最后一个字ASC

rdlzhqv9  于 2023-05-28  发布在  WordPress
关注(0)|答案(1)|浏览(151)

我采取自定义文章类型,并试图显示它的最后一个字的职位标题。从那里,我想传递一个我想在WP_Query中看到的字母的变量(例如“starts_with”=“A”)。最后,我想以ASC顺序显示它。从这个代码,我如何显示在ASC秩序?

function dp_posts_where( $where, $query ) {
    global $wpdb;

    $starts_with = $query->get( 'starts_with' );

    if ( $starts_with ) {
        $last_name = "RIGHT($wpdb->posts.post_title, LOCATE(' ', REVERSE($wpdb->posts.post_title)) - 1)";

        $where .= " AND $last_name LIKE '$starts_with%'";
    }

    return $where;
}

add_filter( 'posts_where', 'dp_posts_where', 10, 2 );

请指教!谢谢你!

ktecyv1j

ktecyv1j1#

你可以尝试这样的东西:

function my_movies_posts_orderby( $orderby, $query ) {
    if ( $query->is_main_query() && $query->is_post_type_archive( 'my-movie' ) ) {
        $orderby = "SUBSTRING_INDEX(post_title, ' ', -1) ASC";
    }
    return $orderby;
}
add_filter( 'posts_orderby', 'my_movies_posts_orderby', 10, 2 );

相关问题