WordPress.org

Ready to get started?Download WordPress

Forums

Issue with sticky posts and WP_Query on front-end (15 posts)

  1. tommycopeland
    Member
    Posted 1 year ago #

    Hey guys! New to PHP here. This is the first theme I ever wrote just about entirely myself and pretty happy with it. However, today I published a new post and noticed something weird, maybe ya'll can give me a tip on how to improve on my code?

    Go to Bullworhy.com and notice in the "issuer alerts" div on the left-hand side of the main content section I've got a line at the top that reads "Genius Brands International (GNUS) Leveraging Digital Footprint To Drive Brand Recognition"...this is a post tagged with the category "Issuer Alerts" and queried using the code below.

    The post below that is sticky, so it's got it's own little section. However, the GNUS post is supposed to show up under the query titled "More Issuer Alerts" just below the title and above the
    "Bullworthy, LLC Initiates Coverage on Oriens Travel & Hotel Management Corp (Pinksheets: OTHM) With Detailed Equity Research Report and $0.10 Price Target".

    Here's the code. Any idea why the GNUS post is showing ABOVE the sticky post and not in the "More Issuer Alerts" section where it should be?? All else being equal of course (i.e. the new GNUS post is tagged correctly, it's not sticky, etc.)

    <div id="issuer-alerts">
      						<?php $my_query = new WP_Query('category_name=issuer-alerts&posts_per_page=10'); while ($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate = $post->ID; ?>
      								<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    									<?php if (is_sticky () ) { ?>
                    					<h1 class="post-title"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h1>
                    					<span class="released">Released on <?php the_date( 'l, F t Y' );?> at <?php the_time( 'g:i a' ); ?></span>
    										<div class="post-entry">
    										<div class="thumb">
                	        				<?php if ( has_post_thumbnail()) : ?>
                	        				<a href="<?php the_permalink(); ?>" ><?php the_post_thumbnail( array('class' => 'alignleft') ); ?></a>
                	        				<?php endif; ?>
                	        				</div><!-- thumb -->
                	        				<div class="excerpt"><?php the_excerpt(); ?>
                	        				<?php $key = 'field_name'; $themeta = get_post_meta($post->ID, 'report_link', true);
                	        				if($themeta != '') { echo '<div class="read-more" style="margin-bottom: 5px;"><a href="' . get_post_meta($post->ID, 'report_link', true) . '" target="_blank">Access This Report Now</a></div>';} ?>
                	        				<div class="read-more"><a href="/issuer-alerts">View All Issuer Alerts</a></div>
                	        				</div><!-- excerpt -->
                	    					</div><!-- end of post-entry -->
                	    					<div class="clear"></div>
                                            <h3>More Issuer Alerts:</h3>
                	    					<?php } else { ?>
    										<li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
    										<?php } ?>
                	    			</div><!-- post-ID -->
                	    	<?php endwhile; ?>
    					</div><!-- issuer-alerts -->
  2. esmi
    Forum Moderator
    Posted 1 year ago #

    In general, you should always issue a wp_reset_query() at the end of each Loop when you are running multiple loops on the same page.

  3. tommycopeland
    Member
    Posted 1 year ago #

    Hi Emsi, thanks! Where exactly would you suggest I put that wp_query in order to achieve the desired outcome of having the post in the UL where it should be?

  4. esmi
    Forum Moderator
    Posted 1 year ago #

    Right after <?php endwhile; ?> in that Loop you posted above.

  5. tommycopeland
    Member
    Posted 1 year ago #

    OK, I added it but that didn't put the GNUS link where it should be. Any other ideas? I'm stumped. I thought the whole point of a "sticky" post was that it would put that post ABOVE all the others, regardless of the date of posting...

  6. esmi
    Forum Moderator
    Posted 1 year ago #

    Can you repost a link to the generated page. I can't seem to locate anything at Bullworhy.com.

  7. tommycopeland
    Member
    Posted 1 year ago #

    Sorry! It was a typo. The link is http://www.bullworthy.com! Thanks!

  8. esmi
    Forum Moderator
    Posted 1 year ago #

    OK - I'm a little confused as to what is supposed to appear where. http://quirm.net/temp/Bullworthy.png is a screen shot of what I'm seeing in Firefox 16 under win 7.

  9. tommycopeland
    Member
    Posted 1 year ago #

    Hopefully this screenshot explains it: http://tommycopeland.fatcow.com/bullworthy/Bullworthy.png

    Basically, I just need the link to the latest post at the top titled "Genius Brands International..." to fall in line with the rest of the un-ordered lists under the headline "More Issuer Alerts". It's showing up where it is now because it's the most recently published post (published yesterday). But I need the one that's sticky, titled "Bullworthy, LLC Releases Bullworthy Equity...." to STAY as the FIRST post ALWAYS. So tomorrow when I go to published another post, unless it's tagged as sticky, the link will show up only in the un-ordered list under the title "More Issuer Alerts" with the rest of the one's that aren't sticky, NOT above the sticky post like it's showing up now. Makes sense?

  10. esmi
    Forum Moderator
    Posted 1 year ago #

    OK - now I can understand the issue. Can you drop a copy of the complete code for that page into a pastebin and post the url here?

  11. tommycopeland
    Member
    Posted 1 year ago #

  12. esmi
    Forum Moderator
    Posted 1 year ago #

    Ah! I think I see the problem Is the GNUS post more recent than the LLC Releases one?

  13. tommycopeland
    Member
    Posted 1 year ago #

    Yes, it is. But the one below it which should be at the top is STICKY...isn't the point of tagging a post as sticky to make it appear as the most recent, even if it's not?

  14. esmi
    Forum Moderator
    Posted 1 year ago #

    From what I recall, sticky posts only rise to the top when using the standard query/loop on the site's main posts page. Since you are using a custom query, you lose that sticky post feature. I think you'll need 2 custom queries & loops - the first to pull just the sticky post and the second to display everything but the sticky post.

    Does that make sense?

  15. tommycopeland
    Member
    Posted 1 year ago #

    It does...not sure how to do it though...I'm just beginning to get a fluent grasp on wp php functions...

Topic Closed

This topic has been closed to new replies.

About this Topic