Support

Account

Home Forums General Issues How to count posts and compare it with a meta field value from custom post type?

Unread

How to count posts and compare it with a meta field value from custom post type?

  • I have created two posts types:

    A) hostals (it has all the meta boxes for a template)
    B) memberships (it has 1 meta box with Name ID “quantity“: packages that is a 3 option radio button) values are 1,5 and 10

    I added a shortcode [count-posts] that links to a function that have to do the following:

    1) Count how many post that USER submitted
    2) Check on the assigned membership to this USER what option was picked, 1, 5 or 10.
    3) Compare them. If the Count was bigger or equal to “Quantity” then the user canĀ“t submit any other post and return “FALSE” if its less than return “TRUE”

    (I am using RTO for dynamic functions, so thats already setup to hide a SUBMIT FORM/BUTTON if shortcode returns false).

    I am not sure how to build this code (I put it in a custom plugin) I got this far but I am not too familiar with codes:

    
    function get_all_users_posts(){
    $args = array(
      'post_type' => 'hostals', // my post type slug
      'post_per_page' => -1, // get them all. I think this is ok
      'post_status' => 'publish', // I think this is ok
      'meta_query' => array( // <strong>STARTING THIS POINT I DONT KNOW WHAT TO DO</strong>
        array(
          'key' => 'your-field-name', // HOW TO CHECK ON A META FIELD FROM ANOTHER POSTTYPE?
          'compare' => 'EXISTS'
        )
      )
    );
    $query = new WP_Query($args);
    $post_count = count($query->posts);
    return $post_count;
    }
    
    add_shortcode('countposts', 'get_all_user_posts');
    
    
Viewing 1 post (of 1 total)

The topic ‘How to count posts and compare it with a meta field value from custom post type?’ is closed to new replies.