Support » Fixing WordPress » wp_query not working correctly

  • Resolved iversoncru

    (@iversoncru)


    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 🙂

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘wp_query not working correctly’ is closed to new replies.