wordpress custom query code example

Example 1: wp_query get custom post type

<?php    
  	   $args = array(  
      'post_type' => 'custom_type',
      'post_status' => 'publish',
      'posts_per_page' => 8, 
      'orderby' => 'title', 
      'order' => 'ASC', 
          );

  $loop = new WP_Query( $args ); 

  while ( $loop->have_posts() ) : $loop->the_post(); 
      print the_title(); 
      the_excerpt(); 
  endwhile;

  wp_reset_postdata(); 
  ?>

Example 2: wp_query to get posts

<?php 
$args = array( 
	'post_type'   => 'post',
	'post_status' => 'future'
);
$scheduled = new WP_Query( $args );

if ( $scheduled->have_posts() ) : 
?>
	<?php while( $scheduled->have_posts() ) : $scheduled->the_post() ?>
		<!-- Display Post Here -->
	<?php endwhile ?>
<?php else : ?>
	<!-- Content If No Posts -->
<?php endif ?>

Example 3: wordpress custom php use wp query

if ( ! defined('ABSPATH') ) {
    /** Set up WordPress environment */
    require_once( dirname( __FILE__ ) . '/wp-load.php' );
}

global $wpdb;
$results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}options WHERE option_id = 1", OBJECT );

Example 4: wp query search

$query = new WP_Query( array( 's' => 'keyword' ) );

Tags:

Misc Example