WordPress.org

Ready to get started?Download WordPress

Forums

wp-query getting unexpected result. Please help! (3 posts)

  1. muydelpalo
    Member
    Posted 1 year ago #

    Hi all!

    I have 5 custom post types that I am showing at my index page. To do so, I have set five <section>, each one containing a loop that retrieves the last 5 posts of a custom post type, like this:

    <?php global $wp_query;
    				query_posts( array(
        			'post_type' => 'cptx',
        			'posts_per_page' => 10 )
    				); ?>
                    <?php if (have_posts() ) : ?>
    				<?php /* Start the Loop */ ?>
    				<?php while ( have_posts() ) : the_post(); ?>
                                        <h1 class="title">
                    						<?php echo get_the_title($ID); ?>
                                        </h1>
                                        <?php /* Some other metadata, etc.... */?>
    									<?php endwhile; ?>
    								<?php else : ?>
    									<?php get_template_part( 'no-results', 'index' ); ?>
    								<?php endif; ?>

    It is working great for the first 4 <section>, but when I get to the fifth, it displays all the posts BUT the ones form that custom post type. The weird thing is I've seen in the database that there is a post from that custom post type, and, before the loop, I have a header for the <section> that outputs the number of posts in the cpt, like this:

    <p class="header-postcount">
    				<?php
    				$count_posts = wp_count_posts('cptx');
    				echo $count_posts->publish; //
    				?>
            		</p>

    And it displays the correct number (1), even though below it fetches 10 no-cptx posts

    What am i doing wrong? Please help!! Thanks!!

  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    it might help if you can paste the full code of the template, i.e. all five queries and everything else, into a pastebin and post the link to it here - http://codex.wordpress.org/Forum_Welcome#Posting_Code

    possibly try to rewrite the queries and loops with WP_Query() instead of query_posts();

    http://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-query-posts-vs-get-posts?rq=1

    example:

    <?php global $wp_query;
    			$query = new WP_Query( array(
        			'post_type' => 'cptx',
        			'posts_per_page' => 10 )
    				); ?>
                    <?php if ($query->have_posts() ) : ?>
    				<?php /* Start the Loop */ ?>
    				<?php while ( $query->have_posts() ) : $query->the_post(); ?>
                                        <h1 class="title">
                    						<?php echo get_the_title($ID); ?>
                                        </h1>
                                        <?php /* Some other metadata, etc.... */?>
    									<?php endwhile; ?>
    								<?php else : ?>
    									<?php get_template_part( 'no-results', 'index' ); ?>
    								<?php endif; wp_reset_postdata(); ?>
  3. muydelpalo
    Member
    Posted 1 year ago #

    Thanks for answering!

    I tried replacing query-posts(); with WP_Query() and $query-> as you explained, but it hasn't worked, all I got was all the five <section> displaying each all the posts, regardless of post type

    Here's my pastebin, sorry the code is quite long, but, as you'll see, there are many if/else statements.

    Just to make sure I haven't done anything wrong that is not showing here, I'll explain the process

    In functions.php I created the 4 first custom post types and metaboxes for them as explained here and here
    As I couldn't get any date and time picker fields to work, I used this method to create the fifth post type in a separate file, and used a

    require( get_template_directory() . '/inc/file.php' );

    to fetch it. But, some of the metaboxes I created in functions.php are also attached to this fifth custom post type

    I hope it is not too messy, but I can't figure out how to do it all at once, I am not that php savvy, but working on it!

    Anyway, thanks a lot for taking your time to read through all this

Topic Closed

This topic has been closed to new replies.

About this Topic