WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Missing posts from query_posts (6 posts)

  1. r_chee
    Member
    Posted 1 year ago #

    Hi all, I'm fairly new to WordPress although I have tinkered with a few pre-made templates to try and get the results I want before. For the first time ever I have embarked on the really fun (yet at the moment frustrating) world of creating a theme from scratch!

    So far I have found it really rewarding, however I have come across a snag that I just can't find the answer for, everything I read, including a tutorial from Lynda.com tells me I'm doing it right - it's just not working as intended. I was hoping someone here would tell me that the problem is really obvious and give me a clue to what is going on.

    Currently I am working off line (using MAMP) to design and set up the theme. I have a fixed home page which displays different categories and a list of the top 1-3 post headings of each one.

    I am using query_posts to select a category and tell it how many posts I want, then a small local loop to get and display them, and then I reset the query ready for the next time.

    I works a treat, although if I tell it to show me a number of posts, it will always show me one less than I ask for - and it always leaves off the latest - which is a major problem for the purpose of the site.

    This happens on all instances of the query_posts loop (lets call it) on the page and it's got be baffled. Here's some mark up of one of the lists...

    <h2 class="side-title">Pick of the day</h2>
    		<?php query_posts('cat=33'.'&showposts=4'.'&orderby=date'	); the_post(); ?>
    		<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    		<div class="latest-post">
    				<div class="recent-thumb"><img src="<?php echo get_post_meta($post->ID, 'thumb_med', true); ?>" /></div>
    				<div class="sidebar-post">
    					<p class="side-cat"><?php the_date(); ?></p>
    					<h5 class="side-name"><?php the_title(); ?></h5>
    				</div>
    		</div>
    		<?php endwhile; endif; ?>
    		<?php wp_reset_query(); ?>

    The above code shows the main title, a thumbnail of the post, it's title and the date. Ok so they are not links yet - I was coming back to that - but it seems to work beautifully, showing only 3 posts (not the coded 4, but I wanted 3!) however not the latest one.

    Does anyone have an idea as to what is going on? There is no main loop on the page anywhere, and just another few of the above snippets where I want the lists to be. Is it just simply that I'm not working on a live site, I wouldn't have thought so but I have to ask!

    Please help someone, show me that I'm daft! :) Thank you in advance.

  2. Marvel Labs
    Member
    Posted 1 year ago #

    why dont you login in dashboard go to Settings>Reading and set the number of blog posts. and call the posts page via regular wp query loop

  3. r_chee
    Member
    Posted 1 year ago #

    Thanks for the quick reply but please excuse my next question...

    I do the above code once on the left of the page to call up 3 post headings and thumbnails, once down the right to call up 5 posts, and several other times throughout the static homepage to show just one. All from different categories.

    If I did what I think you are suggesting wouldn't that dictate how many posts were shown in each list - so I couldn't have different amounts in each list?

    Again, forgive me if I've mis-understood your advice.

  4. Marvel Labs
    Member
    Posted 1 year ago #

    instead of using cat=33 use category name as variable, what i thought was that you were trying to use the template globally, but since you require posts at different places.

    Try this

    ?php
    // The variable query will call 3 latest posts
    $query = 'posts_per_page=3';
    $queryObject = new WP_Query($query);
    // The Loop...
    if ($queryObject->have_posts()) {
    	while ($queryObject->have_posts()) {
    		$queryObject->the_post();
    		the_title();
    		the_content();
    	}
    }
    ?>
  5. r_chee
    Member
    Posted 1 year ago #

    Oooh, close - thank you.

    When I first used your code i got nothing but a blank screen, but after a little fiddling it almost works. It now shows the latest post in that category, but only 1 despite asking for 3.

    Would you mind just quickly casting your eye over what I have done and see if you can see where the error is - just thinking, do I need an array for the query if I want posts_per_page and the category?

    Do I also need to reset the query or is that done automatically as I have specified posts per page as 3?

    Thanks.

    <h2 class="side-title">Pick of the day</h2>
    
    <?php
    $query = 'posts_per_page=3'.'&cat=pickoftheday';
    $queryObject = new WP_Query($query);
    if ($queryObject->have_posts()) {
    	$queryObject->the_post();
    }
    ?>
    		<div class="latest-post">
    		<div class="recent-thumb"><img src="<?php echo get_post_meta($post->ID, 'thumb_med', true); ?>" /></div>
    		<div class="sidebar-post">
    				<p class="side-cat"><?php the_date(); ?></p>
    				<h5 class="side-name"><?php the_title(); ?></h5>
    		</div>
    		</div>
  6. r_chee
    Member
    Posted 1 year ago #

    Sorted. Thank you so much for your advice Marvel Labs. Using what you gave me for inspiration and an evening of reading and tweaking we have it!

    The code I finished with in case anyone else is struggling like i was (nothing worse than a forum cry for help that matches your problem with just a thank you and no details to copy from! :D ) is below. I'm very sure holes can easily be punched in it, but it works.

    Thanks again.

    R

    <h2 class="side-title">Pick of the day</h2>
    
    <?php $posts = get_posts('category=33&numberposts=3'); ?>
    <?php if( $posts ) : ?>
    <?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
    	<div class="latest-post">
    		<div class="recent-thumb"><img src="<?php echo get_post_meta($post->ID, 'thumb_med', true); ?>" /></div>
    
    				<div class="sidebar-post">
    					<p class="side-cat"><?php the_date(); ?></p>
    					<h5 class="side-name"><?php the_title(); ?></h5>
    				</div>
    		</div>
    <?php endforeach; ?>
    <?php endif; ?>
    <?php wp_reset_query(); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic