1

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:

         ?>
1
  • 1
    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
2

$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.

4
  • 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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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