$args = array( 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => -1, ); $query = new WP_Query($args); $count = $query->post_count;
$host = 'localhost'; $dbname = 'database_name'; $user = 'username'; $pass = 'password'; $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $user, $pass); $stmt = $pdo->prepare("SELECT COUNT(*) FROM posts WHERE status = 'published'"); $stmt->execute(); $count = $stmt->fetchColumn();In this example, we are using PDO to connect to the database and execute a SQL query to count the number of posts that are published. This code is not specific to any package library and can be used in any PHP application that uses PDO to interact with the database. Overall, the method used to count posts in PHP depends on the platform or CMS being used and the tools available. There are many different ways to achieve this goal, and the examples provided here are just a few possibilities.