Skip to main content

WP-query a simple query example.

Home Forums WordPress Enqueue Scripts and Styles WP-query a simple query example.

Viewing 0 reply threads
  • Author
    Posts
    • #2395

      Layer7web
      Keymaster
      $args = array(
           'post_type'       => 'post',
           'post_status'     => 'published',
           'category_name'   => 'announcements',
           'posts_per_page'  => 1,
           'order_by'	       => 'date',
           'order'           => 'DESC',
      );
      
      $the_query = new WP_Query( $args );
      
      ob_start();
      
      // The Loop
      if ( $the_query->have_posts() ) {
           echo '<ul>';
           while ( $the_query->have_posts() ) {
                $the_query->the_post();
                echo '<li>' . get_the_title() . '</li>';
           }
           echo '</ul>';
           /* Restore original Post Data */
           wp_reset_postdata();
      } else {
           // no posts found
      }
      $output = ob_get_contents();
      ob_end_clean();
      return $output;
      
Viewing 0 reply threads

You must be logged in to reply to this topic.