The Support Forums will be in read-only mode for a scheduled maintenance window on 01 September 2016 14:00 UTC - 20:00 UTC. More information.

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

  1. virgild
    Posted 5 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>
    		  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

    Posted 5 years ago #

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

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

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

  4. Mark / t31os

    Posted 5 years ago #

    Happy to help.. :)

Topic Closed

This topic has been closed to new replies.

About this Topic