wordpress query code example

Example 1: wordpress execute query

global $wpdb;    
$result = $wpdb->get_results( "SELECT * FROM wp_usermeta WHERE meta_key = 'points' AND user_id = '1'");
print_r($result);

Example 2: wp query

<?php 
// The Query
$the_query = new WP_Query( $args );
 
// 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>';
} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();

Example 3: 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 4: wp query search

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

Tags:

Sql Example