WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to check outside of the loop, if a certain post exists in a specific category and from a specific date.

The function get_posts() -- if I get it right -- allows me to check, if there are posts in a specific category, but does not allow to test, if there is one on a specific date.

The other way I tried is by testing the spefic URL by using the permalink. The permalink has the format: http://www.homepage.de/yyyy/mm/dd/?cat=id. But this URL is valid anyway, if it shows a post or the message "Nothing found." So I can't check the URL for a 404 or something.

Now I ran out of ideas? Any help?

Thanks in advance and all the best, Marius

share|improve this question
up vote 2 down vote accepted

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;
share|improve this answer
Why wouldn't get_posts() work with date arguments? Other than few edge cases it's identical to using WP_Query, which it wraps. – Rarst Nov 11 '13 at 8:17
I wasn't sure about the date arguments, let me edit the answer – Maruti Mohanty Nov 11 '13 at 8:19
Great work! Many thanks! – Marius Nov 11 '13 at 9:16
@Marius If this works for you. can you accept the answer? It will definitely help others looking for the solution to a similar question :) – Maruti Mohanty Nov 11 '13 at 10:25

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.