简体   繁体   中英

WP_Query - Ordering posts by meta type of another post type

I have a custom post type called BID which contains a user's bids (similar to freelancer.com, upwork, etc) However I need the BID results on the page to be ordered by meta stored in a users profile, which is a custom post type PROFILE.

How would I go about doing this?

Here is my current Query

WP_Query( array(  'post_type' => array(BID, PROFILE),
                                'post_parent' => get_the_ID(),
                                'orderby'   => 'meta_value_num',
                                'order'     => 'DESC',
                                'meta_key'  => 'rating_score',
                                'post_status' => array('publish','complete', 'accept', 'unaccept')
                            )
                        );

I don't know of a way to do this with WP_Query alone so I'd probably write some extra PHP to do this. I'm not sure exactly how things are set up so I can't write code, but here's the general idea.

  1. Generate your bid results without paying much attention to how they are ordered.

  2. Loop through your generated query result and, for each item, define a new attribute that fetches the meta information from the corresponding profile meta.

  3. Order your array using this new meta information. You might be able to use PHP's sort() function, or if you need a more general function, try usort()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM