• Resolved Beee

    (@beee)


    I am building a site where user can submit a profile (which is basically an ad).
    The lists need to be ordered by 3 parameters:
    1) by user_status (if user has a certain capability)
    2) by a meta_value from a custom (ACF) field
    3) by (published) date

    2 and 3 is not a problem, but I can’t find how to get the user status in there as well. I don’t want to query just those users with that certain cap, I just want to use it to order it so users who have that cap end up higher in the lists/results.

    This is what I have so far:

    public static function get_profiles( $amount = 24, $exclude = array(), $taxonomy = false, $author = false, $post_status = 'publish', $offset = 0 ) {
    
        if ( $taxonomy ) {
            $tax_query = array(
                array(
                    'taxonomy'  => Taxonomies::SEX,
                    'field'     => 'slug',
                    'terms'     => $taxonomy,
                )
            );
        } else {
            $tax_query = false;
        }
    
        $profile_posts = get_posts(array(
            'post_type'         => PostTypes::PROFILE,
            'post_status'       => $post_status,
            'posts_per_page'    => $amount,
            'meta_key'          => 'sd_move_item_up_timestamp',
            'tax_query'         => $tax_query,
            'author'            => $author,
            'exclude'           => $exclude,
            'offset'            => $offset,
            'orderby'           => array( 'meta_value_num' => 'DESC', 'date' => 'DESC' ),
            'suppress_filters'  => false,
        ));
    
        return $profile_posts;
    
    }

    The order by parameter should be something like this:

    array( 'vip_status' => 'DESC', 'meta_value_num' => 'DESC', 'date' => 'DESC' ),

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    To use WP_User_Query, “vip_status” is not a valid orderby argument. The meta value ordered by must be the one whose key is specified through “meta_key” argument, in your example, ‘sd_move_item_up_timestamp’. To order by different meta data, you need to compose your own SQL clause. You can either insert it to the WP_User_Query SQL through the “pre_user_query” filter, or simply execute your own SQL using $wpdb methods.

    Thread Starter Beee

    (@beee)

    I know it’s not a valid key, it was to illustrate what i would like to achieve.

    I know I need a new clause but I don’t know what it should be. That’s why I came here ??

    Moderator bcworkz

    (@bcworkz)

    OK, sorry for stating the obvious. We get all kinds of skill levels, it’s not so obvious for many. I also misread your topic, thinking you were referring to the user’s author bio, not a profile post, so I started off with the wrong query. Fortunately, other than the exact filter hook, what I said holds true either way.

    Unfortunately, I’m not sure I can properly answer your question. However, it appears that if you reconfigure your query arguments to exclusively use “meta_query” for both meta values and not use the “meta_key” argument at all, the sorting will work despite nothing explicitly requiring ordering by vip_status. The vip_status criteria should merely be EXISTS in the meta_query. The sd_move_item_up_timestamp criteria should be more specific, such as within a particular range with BETWEEN or something like that. When configured this way, I believe using the following orderby argument will result in the order you want even though it’s not explicitly expressed.
    'orderby' => array('meta_value_num' => 'DESC', 'date' => 'DESC' ),

    I don’t know why this seems to work. In my limited testing, ordering by meta_value_num seems to always use the values correlating with the EXISTS key argument. YMMV.

    If all else fails, you could sort the post results using PHP’s usort(). It’s not nearly as efficient, but unless you have a massive amount of profile posts, it shouldn’t make too much difference.

    Thread Starter Beee

    (@beee)

    If it helps, the user status is defined based on a if a user cap is present.

    Thread Starter Beee

    (@beee)

    I created a temporary work-around until I find something better.

    I just create 2 queries now:
    Query posts from vip users
    Query all posts, excluding previous query
    Merge these 2.

    Not ideal, but it does the trick (for now).

    Will close this topic…

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Order by user_status, meta_value, date’ is closed to new replies.