admin管理员组

文章数量:1127659

I have a cpt called "movie", and I set a target for writers to make 50,000 posts in 6 months. The question is... can I make a percentage of the results of the number of recent posts against the target, and how to display it through a shortcode?

For example: "total posts are 50% of target."

Is there any way to display that with a shortcode? Please help.

I have a cpt called "movie", and I set a target for writers to make 50,000 posts in 6 months. The question is... can I make a percentage of the results of the number of recent posts against the target, and how to display it through a shortcode?

For example: "total posts are 50% of target."

Is there any way to display that with a shortcode? Please help.

Share Improve this question asked Dec 22, 2023 at 18:19 Tuagapat ManamTuagapat Manam 415 bronze badges 5
  • you wouldn't use a shortcode to display that inside WP Admin itself, it's far easier and simpler to call the function that displays the percentage directly instead of creating a shortcode that calls it, then constructing a shortcode string dynamically and asking WP to render it. The percentage part is easy as that's just math, it's counting the number of movie posts created in the last 6 months when there are going to be tens of thousands of posts that will be problematic – Tom J Nowell Commented Dec 22, 2023 at 19:13
  • any solutions? plugins? – Tuagapat Manam Commented Dec 22, 2023 at 19:45
  • Does this answer your question? Output number of Wordpress posts remaining until a post milestone is reached – birgire Commented Dec 22, 2023 at 20:56
  • the output that I expect is a percentage, is it possible? – Tuagapat Manam Commented Dec 24, 2023 at 14:53
  • yes, but that would be basic arithmetic that has nothing to do with WordPress e.g. something like this $percentage = ( $found_posts / $target_posts ) * 100; – Tom J Nowell Commented Dec 28, 2023 at 12:48
Add a comment  | 

1 Answer 1

Reset to default 1

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.

本文标签: Display the progress of post achievement with percentage against target in wordpress dashboard