WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] wp_query not working correctly (3 posts)

  1. iversoncru
    Member
    Posted 1 year ago #

    Hi,
    i want to show the posts according to month/year but it is not working.
    My code shows all posts, instead of the posts with a certain month and year.
    it should be so simple...

    $url = curPageURL();
    $yearMonth = substr($url, strrpos($url, '/') - 7, 7);
    $year = substr($yearMonth, 0, 4);
    $monthnum= substr($yearMonth, 5, 2);
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query();
    $args = array(
    	'month' => $monthnum,
    	'year' => $year,
    	'posts_per_page' => 2,
    	'cat'            => 7,
    	'paged'          => $paged,
    );
    //var_dump($args);
    $wp_query->query( $args );
    ?>
    <?php
    	while ( $wp_query->have_posts() ) : $wp_query->the_post();
    
    	$title = get_the_title();
    	if (strlen($title) > 20) {
    		$title = substr($title, 0, 18) . '...';
    	}
    ?>
    <h2><a href="<?php the_permalink(); ?>"><?php echo $title; ?></a>   <?php the_time('F jS, Y') ?></h2>
    <?php the_post_thumbnail('thumbnail');?>
    <p>
    	<?php
    		$excerpt = get_the_excerpt();
    		echo string_limit_words($excerpt,56);
    	?>
    </p>
    <p class="read_more">Read the full post <a href="<?php the_permalink(); ?>">here</a></p>		
    
    <?php endwhile;
    
    	$page_navi = function_exists('wp_pagenavi') ? true : false ;
    	($page_navi) ? wp_pagenavi() : posts_nav_link();
    ?>
    <?php
    	wp_reset_postdata();
    	$wp_query = null; $wp_query = $temp;
    ?>

    I used "var_dump" to check if the wp_query is getting the month and year correctly, and it does.
    so the problem must be after "var_dump"...

    any ideas?

    thanks in advance for your time :)

  2. David Sullivan
    Member
    Posted 1 year ago #

    In your $args array you are saying
    'month' => $monthnum

    This should be
    'monthnum' => $monthnum

    Reference: http://codex.wordpress.org/Class_Reference/WP_Query#Time_Parameters

  3. iversoncru
    Member
    Posted 1 year ago #

    oh god! thanks David
    i cant believe it!
    thanks a loooot

Topic Closed

This topic has been closed to new replies.

About this Topic