Skip to content

Instantly share code, notes, and snippets.

@vielhuber
Last active September 9, 2021 22:55
Show Gist options
  • Save vielhuber/da7711ff20acdb6ac18b15091b48c70d to your computer and use it in GitHub Desktop.
Save vielhuber/da7711ff20acdb6ac18b15091b48c70d to your computer and use it in GitHub Desktop.
get_posts get total count with wp_query / use wp_query without a loop #wordpress
<?php
/* fill in args from https://gist.github.com/luetkemj/2023628 */
$args = [];
$wp_query = new \WP_Query($args);
if($wp_query->found_posts > 0 ) {
foreach($wp_query->posts as $posts__value) {
/* ... */
}
}
// debug query
echo $wp_query->request;
// manually start loop
if ($wp_query->have_posts()) {
while ($wp_query->have_posts()) {
$wp_query->the_post();
echo get_the_title();
}
}
wp_reset_postdata();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment