WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Looping a query in custom widget not working (4 posts)

  1. virgild
    Member
    Posted 3 years ago #

    I'm trying to loop through a query in a widget but I'm getting no results..

    I'm querying the posts table for testing purposes.

    global $wpdb;
    
    		  $widget_query = $wpdb->get_results("SELECT * FROM $wpdb->posts
    			  WHERE post_status = 'published'");
    		  if ($widget_query) :
    			  foreach ($widget_query as $post):
    			  	echo $post->post_title;
    		  ?>
    			  <h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
    		  <?php
    			  endforeach;
    		  else :
    		  ?>
        		  <h2> Not Found</h2>
    		  <?php endif;

    The widget displays fine on the website but I get "Not Found". How can I make this loop work?

  2. Mark / t31os
    Moderator
    Posted 3 years ago #

    The post status for published posts is publish and not published.. ;)

    WHERE post_status = 'published'");
    WHERE post_status = 'publish'");
  3. virgild
    Member
    Posted 3 years ago #

    Lol, I don't think I would have figured that out soon. Thanks t31os you're the best!

  4. Mark / t31os
    Moderator
    Posted 3 years ago #

    Happy to help.. :)

Topic Closed

This topic has been closed to new replies.

About this Topic