Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Ok, I have a client that wants to only display posts by the current month. I've tried a few different variations but none have worked. Here's where I'm at now. Any help is appreciated!

    <?php
    $current_year = date('Y');
    $current_month = date('m');
    $args = array(
        'year'     => $current_year,
        'monthnum' => $current_month
    );
      $custom_query = new WP_Query('$args');

        if ($custom_query->have_posts()) : while ($custom_query->have_posts()) : $custom_query->the_post(); 

                // The following determines what the post format is and shows the correct file accordingly
                $format = get_post_format();
                get_template_part( 'includes/post-formats/'.$format );

                if($format == '')
                get_template_part( 'includes/post-formats/standard' );

         endwhile; else:

         ?>
share|improve this question
    
you're passing the literal string '$args' to WP_Query('$args'), should be WP_Query($args), no quotes. –  Milo Sep 25 '13 at 1:45

1 Answer 1

up vote 1 down vote accepted

$current_month = date('m'); should be $current_month = date('n');

Reference the page for the WP_Query object:

codex.wordpress.org/Class_Reference/WP_Query#Time_Parameters

"month_num" is an int from "1-12", not from "01-12". The way the variables are currently set up, $current_month has a preceding zero. I'm assuming that a simple change to return the month without a preceding zero will fix this. I'm assuming that the query is erroring out because of this improperly formatted argument. Also, make sure as stated in the comments above that you don't put quotes around "$args" in WP_Query.

share|improve this answer
    
Please explain the problem and the solution, rather than just post code. –  s_ha_dum Sep 25 '13 at 0:28
    
Reference the page for the WP_Query object: codex.wordpress.org/Class_Reference/WP_Query#Time_Parameters "month_num" is an int from "1-12", not from "01-12". The way his variables are currently set up, $current_month has a preceding zero. Seeing as there are no other syntax errors, and this is the expected functionality of WP_Query, I'm assuming that a simple change to return the month without a preceding zero will fix this. I'm assuming that the query is erroring out because of this improperly formatted argument. –  Thought Space Designs Sep 25 '13 at 0:36
    
You should edit that into the answer, not post as a comment. –  s_ha_dum Sep 25 '13 at 1:28
    
Tough crowd... updated the answer. –  Thought Space Designs Sep 25 '13 at 1:37

Your Answer

 
discard

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.