最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - Sort custom post archives by a meta value from a different custom post type?

matteradmin7PV0评论

To make a long story short, I have a custom post type called artworks with these meta fields:

title
catalog_no
artist_key

I have a second custom post type called artists with these meta fields:

artist_key
artist_firstname
artist_lastname

Obviously, the artist_key field is used to relate the artist to their works. It contains alphanumeric strings such as "anvcih5-8745".

On category archives pages such as , I want to sort the results by artist_lastname.

In SQL I believe I would achieve this by doing an INNER JOIN, but I'm struggling to understand how to achieve the same thing in Wordpress.

I've figured out that I can add a posts_join filter that will allow me to join to another table. I have written the following hook and confirmed that it's firing on the correct page:

function archives_artwork_join($join)
{
    if ( !is_admin() ) {

        $qo = get_queried_object();
        if ($qo->name == 'sculpture') {
          // modify $join here
        }

        return $join;
    }
}
add_filter( 'posts_join', 'archives_artwork_join' );

But, I don't know how to write the JOIN statement. And I don't even know whether I am on the right track with this approach.

Am I on the right track here, or is there a better approach I could take to sort the artworks category archives by artist last name?

Post a comment

comment list (0)

  1. No comments so far