Skip to content

Instantly share code, notes, and snippets.

@danielpataki
Last active July 8, 2020 20:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save danielpataki/20b83fdcd1e189db0765 to your computer and use it in GitHub Desktop.
Save danielpataki/20b83fdcd1e189db0765 to your computer and use it in GitHub Desktop.
Retrieve Any Post You Want With WP_Query
<?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 ?>
$args = array(
'post_type' => 'post',
'date_query' => array(
array(
'hour' => 9,
'compare' => '>=',
),
array(
'hour' => 17,
'compare' => '<=',
),
array(
'dayofweek' => array( 2, 6 ),
'compare' => 'BETWEEN',
),
),
);
$query = new WP_Query( $args );
<?php if ( have_posts() ) : ?>
<?php while( have_posts() ) : the_post() ?>
<!-- Display Post Here -->
<?php endwhile ?>
<?php else : ?>
<!-- Content If No Posts -->
<?php endif ?>
$args = array(
'post_type' => 'post',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'mood',
'value' => array( 'happy', 'awesome' ),
'compare' => 'IN',
),
array(
'key' => 'income',
'value' => 500,
'compare' => '>',
'type' => 'NUMERIC'
),
),
);
$query = new WP_Query( $args );
$args = array(
'post_type' => array( 'post', 'page' ),
'post_status' => array( 'draft', 'publish' ),
'cat' => 'music,videos'
);
$our_posts = new WP_Query( $args );
$args = array(
'post_type' => 'post',
'tag_id' => '22,92,44,-21'
);
$our_posts = new WP_Query( $args );
$args = array(
'post_type' => 'painting',
'meta_query' => array(
array(
'key' => 'price',
'value' => 50000,
'compare' => '>',
'type' => 'NUMERIC'
),
),
'order_by' => 'meta_value_num',
'meta_key' => 'price'
);
$query = new WP_Query( $args );
$args = array(
'post_type' => 'post',
'post__in' => array( 23, 441, 12, 322, 34, 33),
'order_by' => 'post__in',
);
$query = new WP_Query( $args );
$args = array(
'post_type' => 'post',
'year' => 2013,
'month' => 3
);
$query = new WP_Query( $args );
$args = array(
'post_type' => 'book',
'tax_query' => array(
array(
'taxonomy' => 'genre',
'field' => 'slug',
'terms' => array( 'scifi', 'thriller' ),
'operator' => 'NOT IN',
),
array(
'taxonomy' => 'author',
'field' => 'id',
'terms' => array( 92, 883, 399 ),
),
'relation' => 'AND',
),
);
$query = new WP_Query( $args );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment