How to correctly mark unlocked posts by the user?
-
I have created a custom post type with categories. Users can unlock access to individual posts with the collected points.
I would like to display in the user’s profile (BuddyPress) a list of all posts grouped by category and with the indication of whether the user has unlocked the post.
I have no problem with displaying the list of posts in a given category, it looks like this:
$xyz_args = array( 'post_type' => 'books', 'tax_query' => array( array ( 'taxonomy' => 'books_cat', 'field' => 'slug', 'terms' => 'xyz', ) ), ); $xyz_query = new WP_Query( $xyz_args ); if ( $xyz_query->have_posts() ) { echo 'Posts in category XYZ:'; while ( $xyz_query->have_posts() ) { $xyz_query->the_post(); echo '<a href="' . get_permalink() . '" title="'. get_the_title() . '""><img src="' . get_the_post_thumbnail_url() .'"></a>'; } }
To display all posts unlocked by the user, I use a special mycred function. This is what it looks like:
$user_id = bp_displayed_user_id( ); $purchases = mycred_get_users_purchased_content( $user_id, $number, $order, $ctype ); if ( ! empty( $purchases ) ) { foreach ( $purchases as $entry ) { if (has_term('xyz', 'books_cat', $entry->ref_id)) { // display if the entry is in a specific category echo '<a href="' . mycred_get_permalink( $entry->ref_id ) . '" title="'. mycred_get_the_title( $entry->ref_id ) .'">'; echo '<img src="' . get_the_post_thumbnail_url($entry->ref_id) .'">'; echo '</a>'; } } }
I would like it to look something like this in the user’s profile:
<h4>Posts in category XYZ</h4> <a href="..." class="locked" title="Unlock now">...</a> <a href="..." class="unlocked" title="Post X">...</a> <a href="..." class="unlocked" title="Post Y">...</a> <a href="..." class="unlocked" title="Post Z">...</a> <h4>Posts in category ABC</h4> <a href="..." class="unlocked" title="Post A">...</a> <a href="..." class="locked" title="Unlock now">...</a> <a href="..." class="locked" title="Unlock now">...</a> <a href="..." class="locked" title="Unlock now">...</a>
I don’t know exactly how to put it together. Can anybody help?
- The topic ‘How to correctly mark unlocked posts by the user?’ is closed to new replies.