• Im trying to running a custom query and then based on the post id returned in the loop, run a subquery, however I cant seem to find the variable to do the “if” statement on.

    As follows:

    <?php
    
     $querystr = "
        SELECT wposts.*
        FROM $wpdb->posts wposts
        WHERE  wposts.post_status = 'publish'
        AND wposts.post_type = 'page'
        AND wposts.post_date < NOW()
    	and wposts.id <> 117
        ORDER BY wposts.post_title ASC
     ";
    
     $pageposts = $wpdb->get_results($querystr, OBJECT);
    ?>
     <?php if ($pageposts): ?>
      <?php foreach ($pageposts as $post): ?>
        <?php setup_postdata($post);
    	?>
        <div class="post" id="post-<?php the_ID(); ?>">
          <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
          <?php the_title(); ?></a></h2>
    
    ////// THIS is the code I cant seem to figure out, the variable $post.id isnt valid, what should it be //////////////////////
    
          	<?php if ($post.id == '5') {
    		 echo "You are in Page 5";
    		 }
    		 ?>
    
        </div>
      <?php endforeach; ?>
    
      <?php else : ?>
        <h2 class="center">Not Found</h2>
        <p class="center">Sorry, but you are looking for something that isn't here.</p>
    
     <?php endif; ?>

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter etinteractive

    (@etinteractive)

    nobody?? ??

    Assuming your loop is working the id of the current post is usually obtained with:

    $post->ID;

    so,

    if($post->ID == "5"){
     echo "You are in Page ".$post->ID;
    }
    Thread Starter etinteractive

    (@etinteractive)

    THANK YOU CHURCH!!!!

    works great!!!

    Thread Starter etinteractive

    (@etinteractive)

    Now i have a display problem with this code.

    I have this code and it works, however the display of posts, only shows the last 3.

    Almost like its paginated to 3, as my portfolio page is set to pagination of 3. How is this inheriting this?

    <?php
    
     $querystr = "
        SELECT wposts.*
        FROM $wpdb->posts wposts
        WHERE  wposts.post_status = 'publish'
        AND wposts.post_type = 'page'
        AND wposts.post_date < NOW()
    	and wposts.id <> 117
        ORDER BY wposts.post_title ASC
     ";
    
     $pageposts = $wpdb->get_results($querystr, OBJECT);
    ?>
     <?php if ($pageposts): ?>
      <?php foreach ($pageposts as $post): ?>
        <?php setup_postdata($post);
    	?>
        <div class="post" id="post-<?php the_ID(); ?>">
          <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
          <?php the_title(); ?></a></h2>
    
          <? if($post->ID == "5"){
    		$temp = $wp_query;
    		$wp_query= null;
    		$wp_query = new WP_Query();
    		$wp_query->query('numberposts=25&order=DESC&orderby=post_title&category=1');
    ?>
    		<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
    
            	<?php the_title(); ?>
             <?php endwhile; 
    
    }
         ?>
        </div>
      <?php endforeach; ?>
    
      <?php else : ?>
        <h2 class="center">Not Found</h2>
        <p class="center">Sorry, but you are looking for something that isn't here.
    
     <?php endif; ?>

    The output looks like this:

    Page 1
    Page 2
    Page 3
    Page 4
    Page 5

    • Post 1
    • Post 2
    • Post 3v

    Page 6

    I have 8 posts, but only 3 show up, even though my query says numberposts=25.

    Any ideas???

    I would run your “page 5” query outside your loop, store the results of that query (ie: the titles of the 25 posts) in a variable and the display the contents of that variable in the main loop once page 5 is reached.

    Assuming your main query is working. I would do something like this before the main loop:

    <?php
    $subQuery = new WP_Query('cat=1&numberposts=25&order=DESC&orderby=post_title');
    $wp_query->in_the_loop = true;
    $subQueryOutput = "<ul>";
    while ($subQuery->have_posts()) : $subQuery->the_post();
        $subQueryOutput .= the_title('<li><a href="'.get_permalink($post->ID).'">', '</a></li>', false);
    endwhile;
    $subQueryOutput .= "</ul>";
    
    echo $subQueryOutput;
    ?>

    Make sure 1 is the right category ID and that you are getting more then 3 results when echo $subQueryOutput; is run. If thats working, just take the echo statement out of the above code and place it in your main loop when you check for page 5:

    <? if($post->ID == "5"){ echo $subQueryOutput; } ?>

    It may be overkill but it keeps things modular and avoids you having to worry about nested loops.

    Hope that helps, let me know.

    Thread Starter etinteractive

    (@etinteractive)

    Nice try but same results, it only shows 3 posts.

    it somehow is taking the pagination from my portfolio page.

    If i go into admin and look at all posts with cat=1, i get 8 results.

    Any other ideas???

    Thread Starter etinteractive

    (@etinteractive)

    If i change my admin setting

    Blog pages show at most: from 3 to 100, they all appear.

    So now its all fixed!!!

    Thanks Church! you f’n ROCK!

    ETI

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Custom query based on post id’ is closed to new replies.