0

I was wondering if this is possible.

I have a custom post type call projects that displays a metabox with drop down menu. In that drop down menu I have a few choices listed, draft, open, pending, and completed. Once you select which field you would like, I would like the ability to display a post count of that choice on the front end from the selected author only when they are login.

Example would be author A selects complete 4 times on 4 different posts. So when author A logs onto the site he will see on the front end a number 4 with the word completed.

This is what I am using to display the drop down menu

 array(
            'name'    => __( 'Project Status', 'projects' ),
            'desc'    => __( '', 'projects' ),
            'id'      => $prefix . 'project_select',
            'type'    => 'select',
            'options' => array(
                'Select a Status' => __( 'Select a Status', 'projects' ),
                'Draft' => __( 'Draft', 'projects' ),
                'Open'   => __( 'Open', 'projects' ),
                'Pending'     => __( 'Pending', 'projects' ),
                'Closed'     => __( 'Closed', 'projects' ),
                'Completed'     => __( 'Completed', 'projects' ),
            ),
0

Try this code:

function postsCount($meta_value) {
    $args = array(
        'numberposts'   => -1,
        'post_type'        => 'custom_post_type', // set you custom post type
        'meta_key'         => 'project_select',
        'meta_value'       => $meta_value,
    );

    $my_posts = get_posts( $args ); 
    $postsCount = count($my_posts);
    return $postsCount;
}
if ( is_user_logged_in() ) {
    echo postsCount('Draft');
    echo postsCount('Completed');
}

or if you want to get all counts by less code, try this:

function postsCount() {
    if ( !is_user_logged_in() ) {
        return;
    }
    $meta_value_array = array( 'Draft', 'Open', 'Pending', 'Closed', 'Completed');
    $postsCount = '<ul>';
    foreach ($meta_value_array as $meta_value) {
        $args = array(
            'numberposts'   => -1,
            'post_type'        => 'custom_post_type', // set you custom post type
            'meta_key'         => 'project_select',
            'meta_value'       => $meta_value,
        );

        $my_posts = get_posts( $args ); 
        $postsCount = count($my_posts);
        $postsCount .= '<li>' .$meta_value. ' ' .$postsCount. '</li>';
    }
    $postsCount .= '</ul>';
    return $postsCount;
}

echo postsCount();
  • Please add a proper explanation to what your code does and how it works. This is considered code dumping which is not allowed – Pieter Goosen Jun 27 '14 at 6:07
  • I tried both codes and they do not display a count for the selections. Both codes just display a 0 @AdamMo. – user3756781 Jun 27 '14 at 20:22
  • 1
    Sorry I figured it out I had to change the meta_key. Thank you @AdamMo. – user3756781 Jun 27 '14 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.