0

EDIT: I made some research and this is where I stand now:

I'm using custom post types to let users add own content to my site. The user could also sign a paid membership (made with WooCommerce Membership plugin). This process works very well.

Now I want to display all the posts from the users in an overview. Starting with posts from paying members.

Membership plans and signed memberships are stored as custom post types. The user of a signed membership is also the post author.

So I guess I need to use the membership users/authors in the loop for my post types. To do this I've a loop which returns all author IDs of active memberships and store them in an array. The code looks like this:

<ul>
    <?php
    $posts = get_posts( array(
        'post_type'     => 'wc_user_membership',
        'post_status'   => array('wcm-active'),
        'numberposts'   => -1,
    ) );

    $ids = array();

    foreach ( $posts as $post ) :
        setup_postdata( $post ); ?>
        <?php
            $post_author_id = get_post_field( 'post_author', $post_id );
            array_push( $ids, get_the_author_meta('ID', $post_author_id) );
        ?>
    <?php endforeach; wp_reset_postdata(); ?>
</ul>

<?php print_r($ids); ?>

And the output looks like this:

Array ( [0] => 545 [1] => 543 [2] => 542 [3] => 538 [4] => 6 )

At the moment, I'm displaying posts separated by the category.

For example: I've 10 categories and I show 6 posts per category before a new post list for the next category begins. This part also works fine.

My code looks like this at the moment:

<?php 
    // Here I get the terms to display
    $post_type_categories = get_terms( 'post_type_category' ); 
?>

<ul class=" list-unstyled">
<?php 
    // Here I show 6 posts per term from above
    foreach ( $post_type_categories as $post_type_category ) {
    $post_type_category_query = new WP_Query( array(
        'post_type' => 'post_type',
        'orderby' => 'date',
        'order' => 'DESC',
        'posts_per_page' => 6,
        'tax_query' => array(
            array(
                'taxonomy' => 'post_type_category',
                'field' => 'slug',
                'terms' => array( $post_type_category->slug ),
                'operator' => 'IN'
            )
        )
    ) );
?>
<li class="mb-5">
    <p class="h4"><?php /* Display the term title as headline */ echo $post_type_category->name; ?></p>
    <ul class="list-unstyled row">
    <?php 
    // Show posts from the category
    // Here I want to display the posts from an author with a specific membership plan (meta data?!) first   
    if ( $post_type_category_query->have_posts() ) : while ( $post_type_category_query->have_posts() ) : $post_type_category_query->the_post(); ?>
        <li class="col-lg-4">
            <strong><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute('echo=0'); ?>"><?php the_title(); ?></a></strong>
        </li>
    <?php endwhile; endif; ?>
    </ul>
</li>
<?php $post_type_category_query = null; wp_reset_postdata(); } ?>
</ul>

It displays a list of categories and 6 posts per category.

Is there any way to combine these two loops and show the posts from the members on top?

I guess I need to use the author parameters from the WP_Query (https://codex.wordpress.org/Class_Reference/WP_Query#Author_Parameters) to do so?

I'm able to show only posts from the members but I don't know how to combine these posts and sort them. Also there should not be duplicates in the loop (member posts on top and in the rest of the posts)

3
+25

I would flag post as member_post when it is submitted to database and use this flag to sort them.

function wpse_287048_flag_member_post( $post_id ) {

    if ( wc_memberships_get_user_active_memberships() !== false ) { // Check if user is member
        update_post_meta( $post_id, 'member_post', 1 );
    } else {
        update_post_meta( $post_id, 'member_post', 0 );
    }
}

add_action( 'save_post', 'wpse_287048_flag_member_post' );

Then you are free to retrive your post sorted by member_post.

function wpse_287048_get_posts() {

    $query = new WP_Query(array(
        'post_type' => 'post',
        'meta_key' => 'member_post',
        'orderby' => 'meta_value_num post_date',
    ));

    return $query->get_posts();
}

Keep in my mind that all posts which you want to retrive must have member_post meta_key with some value otherwise they would not be selected from database.

  • I've already a lot of active memberships and also two different membership plans (silver and gold). would it be possible to sort them only with member_post? (gold -> silver -> free) – Cray Nov 30 '17 at 12:42
  • 1
    You can set member_post meta field to 2 for gold users to 1 for silver and 0 to free. And sort by member_post descending. – kierzniak Nov 30 '17 at 12:51
  • OK, I've to check that. I also have to add the value to every existing member, correct? – Cray Nov 30 '17 at 17:27
  • You are asking about member_post meta? You have to add it to every post which you want to display. 2 for posts which was add by gold users 1 for posts which was add by silver users and 0 which was add by free users. – kierzniak Nov 30 '17 at 20:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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