Display the progress of post achievement with percentage against target in wordpress dashboard

Here are my thoughts, which include both a shortcode and a direct function to display the post progress in the WordPress dashboard/admin area:

  1. Creating a Custom Shortcode:

    You can create a custom shortcode to display the post progress on your WordPress site using the following code:

    function post_progress_shortcode() {
        $post_type="movie";
        $target_count = 50000; // Set your target post count here
    
        $args = array(
            'post_type' => $post_type,
            'post_status' => 'publish',
        );
    
        $query = new WP_Query($args);
        $post_count = $query->found_posts;
    
        if ($post_count > 0) {
            $percentage = ($post_count / $target_count) * 100;
            $percentage = round($percentage, 2); // Round to 2 decimal places
            return "Total posts are {$percentage}% of the target.";
        } else {
            return "No posts found.";
        }
    }
    add_shortcode('post_progress', 'post_progress_shortcode');
    

    Usage: You can use the [post_progress] shortcode anywhere in your content to display the progress. This will allow you to display these statistics on the frontend of your website wherever you like.

  2. Function for Dashboard/Admin Area:

    If you want to display the post progress directly in the WordPress dashboard/admin area, you can add the following code to your theme’s functions.php file or a custom plugin file:

    function display_post_progress() {
        $post_type="movie";
        $target_count = 50000; // Set your target post count here
    
        $args = array(
            'post_type' => $post_type,
            'post_status' => 'publish',
        );
    
        $query = new WP_Query($args);
        $post_count = $query->found_posts;
    
        if ($post_count > 0) {
            $percentage = ($post_count / $target_count) * 100;
            $percentage = round($percentage, 2); // Round to 2 decimal places
            echo "<p>Total posts are {$percentage}% of the target.</p>";
        } else {
            echo "<p>No posts found.</p>";
        }
    }
    
    // Hook the function to display in the dashboard
    function add_post_progress_dashboard_widget() {
        wp_add_dashboard_widget(
            'post_progress_widget',
            'Post Progress',
            'display_post_progress'
        );
    }
    add_action('wp_dashboard_setup', 'add_post_progress_dashboard_widget');
    

    This will add a widget titled “Post Progress” to the WordPress dashboard displaying the progress.

Now you have two methods to display post progress, one as a shortcode for content and the other as a dashboard widget for the admin area. You can use either method depending on your preference.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)