I prefer to use wp_query
, which gives you a lot more flexibility to custom query.
As your requirement is only to check if there are posts with a particular category and date you can write a custom function and can use it anywhere to check it.
Use the code below in the active theme’s functions.php
file
function check_post_cat_date( $post_type="post", $category = 'uncategorized', $published_on = '12-31-2012' ) {
// Use the date format as (mm-dd-yyyy) else change accordingly
$date = explode( '-', $published_on );
$args = array(
'post_type' => $post_type,
'category_name' => $category,
'monthnum' => (int) $date[0],
'day' => (int) $date[1],
'year' => (int) $date[2],
);
$the_posts = new WP_Query( $args );
return count( $the_posts->posts );
}
And if you are using version 3.7 or above you can use the date_query
Now you can use this function to check the number of posts it returns.
You can use it in anywhere, something like below:–
$post_type="post";
$category = 'uncategorized';
$published_on = '10-3-2013';
$user_query = check_post_cat_date( $post_type, $category, $published_on );
if ( $user_query ) {
// Do something
echo 'There are ' . $user_query . 'post(s) with category: ' . $category . ' published on ' . $published_on;
} else {
// Do something else
echo 'There are no posts with category: ' . $category . ' published on ' . $published_on;
}