Forums

[resolved] WP_Query not working correctly, need some help. (10 posts)

  1. geezerd
    Member
    Posted 1 year ago #

    Hi, all.

    I just installed MU, and have one blog running on it fine, but then the 2nd blog went up, and this code in the themes header.php no longer worked as intended on the 2nd blog (still works in the 1st blog:

    <?php
    $featuredPosts = new WP_Query();
    $featuredPosts->query('showposts=1&sticky_posts');
    while ($featuredPosts->have_posts()) : $featuredPosts->the_post(); ?>
    <div id="post-<?php the_ID(); ?>">
    <h1><?php the_title(); ?></h1>
    <div class="entry"><?php the_content('More...'); ?></div>
    <?php endwhile; ?>

    In the 1st blog, it's showing the latest sticky post of the blog just fine and dandy. But on the 2nd blog, it keeps ALSO showing the latest post of the blog as well (and it's NOT set to sticky). So it throws in an extra post. But not on the 1st blog, just the 2nd. Both blogs share the same theme.

    What's going on? Should I use a different query? What then? Thanks in advance!

  2. vtxyzzy
    Member
    Posted 1 year ago #

    Unless I am mistaken, the query should be this:

    $stickies=get_option('sticky_posts') ;
    if ($stickies) {
       $args = array(
          'posts_per_page' => 1,
          'post__in'  => $stickies,
          'caller_get_posts' => 1
       );
       $featuredPosts = new WP_Query();
       $featuredPosts->query($args);
    }
  3. geezerd
    Member
    Posted 1 year ago #

    Thanks, it almost works, lol. It only shows the title, but not the content of the post.

    This how I put it in the header:

    <?php
    $stickies=get_option('sticky_posts') ;
    if ($stickies) {
       $args = array(
          'posts_per_page' => 1,
          'post__in'  => $stickies,
          'caller_get_posts' => 1
       );
       $featuredPosts = new WP_Query();
       $featuredPosts->query($args);
    }; ?>
    <div id="post-<?php the_ID(); ?>">
    <h1><?php the_title(); ?></h1>
    <div class="entry"><?php the_content('More...'); ?></div>
    </div>

    Did I do something wrong?
    Thanks

  4. vtxyzzy
    Member
    Posted 1 year ago #

    I believe you left out a line from what you originally posted. Try this:

    <?php
    $stickies=get_option('sticky_posts') ;
    if ($stickies) {
       $args = array(
          'posts_per_page' => 1,
          'post__in'  => $stickies,
          'caller_get_posts' => 1
       );
       $featuredPosts = new WP_Query();
       $featuredPosts->query($args);
       while ($featuredPosts->have_posts()) : $featuredPosts->the_post();
    }; ?>
    <div id="post-<?php the_ID(); ?>">
    <h1><?php the_title(); ?></h1>
    <div class="entry"><?php the_content('More...'); ?></div>
    </div>
  5. geezerd
    Member
    Posted 1 year ago #

    No, that gives me an error:
    Parse error: syntax error, unexpected '}' in /home/content/a/l/a/alanhblume/html/blogs/wp-content/themes/SUSblog/header.php on line 68

  6. vtxyzzy
    Member
    Posted 1 year ago #

    Sorry, should have moved the closing brace for the if:

    <?php
    $stickies=get_option('sticky_posts') ;
    if ($stickies) {
       $args = array(
          'posts_per_page' => 1,
          'post__in'  => $stickies,
          'caller_get_posts' => 1
       );
       $featuredPosts = new WP_Query();
       $featuredPosts->query($args);
       while ($featuredPosts->have_posts()) : $featuredPosts->the_post(); ?>
          <div id="post-<?php the_ID(); ?>">
             <h1><?php the_title(); ?></h1>
             <div class="entry"><?php the_content('More...'); ?></div>
          </div>
       <?php endwhile;
    }; ?>
  7. geezerd
    Member
    Posted 1 year ago #

    Great! Thank you!

    ...and while I have you... and you seam to like a challenge... ;)

    No one has replied to these 2 questions I posted earlier:

    http://mu.wordpress.org/forums/topic/18496?replies=0

    http://wordpress.org/support/topic/415321?replies=1

    If you happen to know either of those answers, I'd appreciate it.

    Thanks again, my friend!

  8. vtxyzzy
    Member
    Posted 1 year ago #

    The first link gives a 'Topic not found' error. As for the second, I had looked at it earlier, but I don't know enough Java to be able to help.

  9. geezerd
    Member
    Posted 1 year ago #

    Not a problem, thanks for looking!

  10. vtxyzzy
    Member
    Posted 1 year ago #

    You are welcome! Now, please use the dropdown at top right to mark this topic 'Resolved'.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.