Skip to content

Instantly share code, notes, and snippets.

@raazon
Last active October 20, 2021 22:34
Show Gist options
  • Select an option

  • Save raazon/8912234efab5e1901b232aae63a92b04 to your computer and use it in GitHub Desktop.

Select an option

Save raazon/8912234efab5e1901b232aae63a92b04 to your computer and use it in GitHub Desktop.
WooCommerce result count / Post result count
<?php
/**
* WorpPress post query result
* @since 1.0.0
* @author Razon Komar Pal
* @return string - like: Showing 1–12 Of 29 Results
*/
if (get_query_var('paged')) {
$paged = get_query_var('paged');
} elseif (get_query_var('page')) { // 'page' is used instead of 'paged' on Static Front Page
$paged = get_query_var('page');
} else {
$paged = 1;
}
$postsPerPage = 10;
$args = [
'post_type' => 'product', // any post type
'post_status' => 'publish',
'posts_per_page' => $postsPerPage,
'paged' => $paged,
];
$product_query = new WP_Query($args);
$total = $product_query->found_posts;
$current = $paged;
if ($total <= $postsPerPage || -1 === $postsPerPage) {
/* translators: %d: total results */
printf(_n('Showing the single result', 'Showing all %d results', $total, 'text_domain'), $total);
} else {
$first = ($postsPerPage * $current) - $postsPerPage + 1;
$last = min($total, $postsPerPage * $current);
printf(_nx('Showing the single result', 'Showing %1$d&ndash;%2$d of %3$d results', $total, 'with first and last result', 'text_domain'), $first, $last, $total);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment