1

Is it possible to order posts retrieved via WP_Query according to a meta value of the corresponding post author?

Use case: I have premium subscribers and basic subscribers. I want the posts of the premium subscribers to appear before the posts of the basic subscribers in a search result page.

One way to solve this, would be to add a meta_key to the post, indicating the author's subscriber status, but I would have to update all posts of a user everytime this user's status changes.

Is there a way to make this work by referring to the user meta values directly?

$args = array(
            'post_status' => 'publish',
            'post_type' => 'listing',
            'meta_key' => '???', // how can I refer to the meta_key of the author of a post ?
            'orderby' => '???',
            'order' => 'DESC'
        );

$query = new WP_Query($args);
2
  • How is premium subscribers and basic subscribers saved in the db? These must be relationship here to the author. There should be some value saved in the db that will distinguish a premium subscriber from a basic subscribers Jun 3, 2014 at 13:46
  • yes, in wp_usermeta there is the account (meta_key) with the meta_values 1 for basic and 2 for premium.
    – yumba
    Jun 3, 2014 at 13:50

2 Answers 2

3

This should give you an idea and I believe this should help you forward.

This query first uses WP_User_Query to fetch all users that are authors registered as premium subscribers and basic subscribers. These users are ordered by meta_value so that premium subscribers are shown first.

A foreach loop return each users' own ID which can be returned into WP_Query to display posts for each user in order of the meta_value

Here is the complete code. Modify it as needed

<?php
$args = array(
    'who' => 'authors',
    'meta_key' => 'account',
    'orderby' => 'meta_value',
    'order' => 'DESC',
    'meta_query' => array(
        array(
            'key' => 'account',
            'value' => array(1, 2),
        )
    )
);

// The WP_User_Query
$user_query = new WP_User_Query( $args );

// User Loop
if ( ! empty( $user_query->results ) ) {
    foreach ( $user_query->results as $user ) {
        $user->ID;

        $the_query = new WP_Query( 'author=' . $user->ID . '&post_status=publish&post_type=listing' );
        echo '<p>' . $user->display_name . '</p>';
        // The Loop
        if ( $the_query->have_posts() ) {
            echo '<ul>';
            while ( $the_query->have_posts() ) {
                $the_query->the_post();
                echo '<li>' . get_the_title() . '</li>';
            }
            echo '</ul>';
        } 
        /* Restore original Post Data */
        wp_reset_postdata();
    }
}
?>
0

If I understand correctly, this is what you're looking for:

You will need to specify for each post, it's "subscriber_status" (Which will need to be a custom meta field). You will also need to assign a custom meta field to each author for their respective "subscriber_status".

$current_user = wp_get_current_user();

// This represents the custom meta field associated with the user data
$subscriber_status = get_the_author_meta('subscriber_status', $current_user->ID);

// This will query against the custom meta field associated with the post data
$query = new WP_Query([
    'post_type'   => 'listing',
    'post_status' => 'publish',
    'meta_key'    => 'subscriber_status',
    'meta_value'  => $subscriber_status,
    'order_by'    => 'meta_value',
    'order'       => 'DESC',
]);

Hopefully that helps you out.

More on wp_get_current_user()

More on get_the_author_meta()

More on WP_Query

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.