Search code examples
wordpresswordpress-shortcode

Adjust wordpress shortcode to present a user type


I am running a job board with WPJM on wordpress.

Do anyone know how to create a shortcode to display number of employer accounts registered?

I use the following code to count published jobs, and maybe its just to tweak it a bit? (I found this after googleing and it works)

function wpb_total_posts() { 
$total = wp_count_posts($type = 'job_listing')->publish;
return $total; 
} 
add_shortcode('total_posts','wpb_total_posts');

I use the mentioned shortcode to present job listings on my website, however I dont know how to change it so it presents amount of registered users (employers) instead.

My guess is to change the 'job_listing' to something about the user type... perhaps 'user_employer' and perhaps change the publish word to something else, perhaps "approved".

Best regards,

Oskar

EDIT:

I found this code, but not sure what all the different things means and what to edit to make it work:

// Function to show the user count by role via shortcode
function wpb_user_count($atts) {
$atts = shortcode_atts( array(
‘role’ => ”
), $atts );
$user_query = new WP_User_Query( array( ‘role’ => $atts[‘role’] ) );
// Get the total number of users for the current query. I use (int) only for sanitize.
$result = (int) $user_query->get_total();
return $result;
}
// Creating a shortcode to display user count
add_shortcode(‘user_count’, ‘wpb_user_count’);

// Use this Shortcode to show user [user_count role=”Subscriber”]

Solution

  • As I understand, the main problem is using incorrect apostrophes. Here is the corrected version:

    // Function to show the user count by role via shortcode
    function wpb_user_count($atts) {
        $atts = shortcode_atts( array(
            'role' => ''
        ), $atts );
        $user_query = new WP_User_Query( array( 'role' => $atts['role'] ) );
        // Get the total number of users for the current query. I use (int) only for sanitize.
        $result = (int) $user_query->get_total();
        return $result;
    }
    // Creating a shortcode to display user count
    add_shortcode('user_count', 'wpb_user_count');
    
    // Use this Shortcode to show user [user_count role="Subscriber"]
    

    Do you have any other problems?