WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Looping a query in custom widget not working

[Resolved] Looping a query in custom widget not working

  • 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?

Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Resolved] Looping a query in custom widget not working’ is closed to new replies.