WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] query_posts isnt working with next_posts_link (5 posts)

  1. dmayman
    Member
    Posted 4 years ago #

    Hi,

    I'm in the middle of making my archive.php page for my template. The way I'm organizing my data is that there will be 2 parent categories for all the other categories: 'blog'and 'portfolio'. I basically want it so that on the blog page, only posts of categories who's parent is 'blog' are shown, and same for portfolio.

    I got this working just fine by using this code:

    global $wp_query;
    	query_posts(
    		array_merge(
    			array('category_name' => 'Blog'),
    			$wp_query->query
    		)
    	);

    so that it merges the previous query and adds the request to only show entries with a category name 'blog'.

    Here's the problem. I wanted to test out how it looked with multiple pages of posts, so I set my posts per page (in the Dashboard settings > Reading) to 2. Right now I have 4 posts with categories with the 'Blog' parent and 3 with 'Porfolio'.

    In my section where I have the tags previous_posts_link() and next_posts_link(), it's actually giving me 4 pages until my "Older Entries" button disappears. Those last 2 pages, though, are empty (besides everything outside of The Loop). It's accommodating for all 7 posts for some reason. Is there any way to make previous_posts_link() and next_posts_link() react correctly to my new query?

    I hope I explained this correctly. Thanks for your help!

    David

  2. dmayman
    Member
    Posted 4 years ago #

    Anybody have any idea?

  3. Sayontan Sinha
    Member
    Posted 4 years ago #

    Can you post a link to the page?
    I am trying to understand if there is any merit in merging the results. What happens if you simply fetch the result by querying for category name "Blog"? It is pretty normal to have categories form headers of pages (or be listed in the sidebar) and I am not sure you need to merge.

  4. dmayman
    Member
    Posted 4 years ago #

    If I don't merge, the page will ignore the user's request of how to display the content. For example, if the user clicks "August 2009" the page will still display all the posts in the 'Blog' category, not just 'Blog' posts that are also in August. It also starts from the first post in the results on each page, like page 2 would show posts 1 and 2 vs posts 3 and 4, as well as any page beyond that.

    Here's the page:
    http://dg2m.com/wordpress/2009/

    It's still not very operational. The only links that really work are the date links and the category links, since I've only created my archive.php page so far.

  5. dmayman
    Member
    Posted 4 years ago #

    I think I've got it. It's all working now, so I'll update if it stops working. For those with the same problem, here's what I did.

    I'm basically combining the old wp_query's query with my new additions (only allowing 'Blog' categories through). This way, if I click on "August" in the blog section, it's saying "Here's every post in August that is ALSO in 'blog'"

    My problem earlier was that the $wp_query variable was still intact, so any functions that used it (next_posts_link, etc.) were using the OLD query, which included more posts than I wanted. So at the end of my code I dump the old wp_query and fill it with my new combined query. It's working so far.

    Check out the code:

    global $wp_query; //grab the old wp_query
    	$newQuery = array_merge( //merge the old and new into $newQuery array
    		array('category_name' => 'Blog'),
    		$wp_query->query
    		);			
    
    $wp_query= null; //empty the old query
       $wp_query = new WP_Query(); //reinitialize the object
       $wp_query->query($newQuery); //finally, tell WP to make a query with our new variable

    Hope this helps anybody in the same situation!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.