Skip to content

Instantly share code, notes, and snippets.

@zgordon
Created March 2, 2014 15:57
Show Gist options
  • Save zgordon/9308580 to your computer and use it in GitHub Desktop.
Save zgordon/9308580 to your computer and use it in GitHub Desktop.
WP_Query Loop for GET AJAX Request
<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');
$post_type = $_GET['post_type'];
$time = $_GET['time'];
$order = $_GET['order'];
$page_num = $_GET['page'];
if( $post_type != 'everything' ) {
$post_type = $post_type;
} else {
$post_type = array('scrib', 'story');
}
// Don't display private posts
if( $post_type != 'scrib' ) {
$meta_query = array(
array(
'key' => 'story_privacy',
'value' => 1,
'compare' => '!='
)
);
$args['meta_query'] = $meta_query;
}
if( $time == 'today' ) {
$year = date('Y');
$monthnum = date('n');
$day = date('d')-1;
} elseif ( $time == 'thisweek' ) {
$year = date('Y');
$week = date('W')-1;
} elseif ( $time == 'thismonth' ) {
$year = date('Y');
$monthnum = date('n');
} else {}
if( $order == 'date' ) {
$orderby = 'date';
$sort = 'DESC';
} else {
$orderby = 'title';
$sort = 'ASC';
}
$args = array (
'post_type' => $post_type,
'paged' => $page_num,
'year' => $year,
'monthnum' => $monthnum,
'day' => $day,
'w' => $week,
'orderby' => $orderby,
'order' => $order,
);
// The Query
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
// Include the page content template.
if ( get_post_type( $post->ID ) == 'story' ) {
get_template_part( 'content', 'stories' );
} else {
get_template_part( 'content', 'scribs' );
}
endwhile; endif;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment