Skip to content

Instantly share code, notes, and snippets.

@gvgvgvijayan
Created January 3, 2021 11:20
Show Gist options
  • Save gvgvgvijayan/1a0182bc3ed63e07964f0a2494d3fd65 to your computer and use it in GitHub Desktop.
Save gvgvgvijayan/1a0182bc3ed63e07964f0a2494d3fd65 to your computer and use it in GitHub Desktop.
<?php
...
/**
* Return instances post object.
*
* @return WP_Query Custom query object with passed arguments.
*/
protected function get_posts_object() {
$post_types = $this->allowed_post_types;
$post_args = array(
'post_type' => $post_types,
'post_status' => array( 'draft' ),
'posts_per_page' => self::POSTS_PER_PAGE,
);
$paged = filter_input( INPUT_GET, 'paged', FILTER_VALIDATE_INT );
if ( $paged ) {
$post_args['paged'] = $paged;
}
$post_type = filter_input( INPUT_GET, 'type', FILTER_SANITIZE_STRING );
if ( $post_type ) {
$post_args['post_type'] = $post_type;
}
$orderby = sanitize_sql_orderby( filter_input( INPUT_GET, 'orderby' ) );
$order = esc_sql( filter_input( INPUT_GET, 'order' ) );
if ( empty( $orderby ) ) {
$orderby = 'date';
}
if ( empty( $order ) ) {
$order = 'DESC';
}
$post_args['orderby'] = $orderby;
$post_args['order'] = $order;
$search = esc_sql( filter_input( INPUT_GET, 's' ) );
if ( ! empty( $search ) ) {
$post_args['s'] = $search;
}
return new \WP_Query( $post_args );
}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment