• Resolved radzio1615

    (@radzio1615)


    Hi!
    How to count user unlocked posts in a specific custom taxonomy term?

    I know how to get count of all post in a specific custom taxonomy term:

    $args = array(
        'post_type' => 'books',
        'tax_query' => array(
            array (
                'taxonomy' => 'books_cat',
                'field' => 'slug',
                'terms' => 'adventure',
            )
        ),
    );
    
    $the_query = new WP_Query( $args );
    $the_query->found_posts;

    But how to count only unlocked posts by the user in that taxonomy term?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter radzio1615

    (@radzio1615)

    I know also that mycred_user_paid_for_content() function can check if post is unlocked by the user. But how to use that function to count posts?

    if ( $the_query->have_posts() ) {
        echo 'X out of '. $the_query->found_posts . ' unlocked';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            if ( ! mycred_user_paid_for_content( $user_id, $the_query->post->ID ) ) {
                // Post not unlocked by the user
            } else {
                // Post unlocked by the user
            }
        }
    }
    
    Thread Starter radzio1615

    (@radzio1615)

    Maybe mycred_get_users_purchased_content function can be helpful, but I don’t know how to combine it with my code. Any idea?

    Hi @radzio1615,

    Thank you for contacting us, You can use it like that.

    count( mycred_get_users_purchased_content( get_current_user_id(), 0, 'DESC', 'mycred_default' ) )

    Thread Starter radzio1615

    (@radzio1615)

    Thank you for your response.

    Unfortunetly, this code counts all posts unlocked by the user.
    I want to count posts from one specific category.

    Hi @radzio1615,

    The above function will get all the logs for the payments and the reference id there is the post id you can use to achieve what you are looking for.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to count user unlocked posts?’ is closed to new replies.