Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save carlaizumibamford/0d0ab6de54acc905ae90aaa67690d766 to your computer and use it in GitHub Desktop.
Save carlaizumibamford/0d0ab6de54acc905ae90aaa67690d766 to your computer and use it in GitHub Desktop.
Display x posts per page:
$query = new WP_Query( array( 'posts_per_page' => 3 ) );
Display all posts in one page:
$query = new WP_Query( array( 'posts_per_page' => -1 ) );
Display all posts by disabling pagination:
$query = new WP_Query( array( 'nopaging' => true ) );
Display posts from the 4th one:
$query = new WP_Query( array( 'offset' => 3 ) );
Display 5 posts per page which follow the 3 most recent posts:
$query = new WP_Query( array( 'posts_per_page' => 5, 'offset' => 3 ) );
Display posts from page number x:
$query = new WP_Query( array( 'paged' => 6 ) );
Display posts from current page:
$query = new WP_Query( array( 'paged' => get_query_var( 'paged' ) ) );
Display posts from the current page and set the ‘paged’ parameter to 1 when the query variable is not set (first page).
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$query = new WP_Query( array( 'paged' => $paged ) );
Pagination Note: Use get_query_var('page'); if you want your query to work in a page template that you’ve set as your static front page. The query variable ‘page’ also holds the pagenumber for a single paginated Post or Page that includes the <!--nextpage--> quicktag in the post content.
Display posts from current page on a static front page:
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$query = new WP_Query( array( 'paged' => $paged ) );
Display just the first sticky post:
$sticky = get_option( 'sticky_posts' );
$query = new WP_Query( array( 'p' => $sticky[0] ) );
Display just the first sticky post, if none return the last post published:
$args = array(
'posts_per_page' => 1,
'post__in' => get_option( 'sticky_posts' ),
'ignore_sticky_posts' => 1,
);
$query = new WP_Query( $args );
Display just the first sticky post, if none return nothing:
$sticky = get_option( 'sticky_posts' );
$args = array(
'posts_per_page' => 1,
'post__in' => $sticky,
'ignore_sticky_posts' => 1,
);
$query = new WP_Query( $args );
if ( $sticky[0] ) {
// insert here your stuff...
}
Exclude all sticky posts from the query:
$query = new WP_Query( array( 'post__not_in' => get_option( 'sticky_posts' ) ) );
Exclude sticky posts from a category:
Return ALL posts within the category, but don’t show sticky posts at the top. The ‘sticky posts’ will still show in their natural position (e.g. by date):
$query = new WP_Query( array( 'ignore_sticky_posts' => 1, 'posts_per_page' => 3, 'cat' => 6 );
Exclude sticky posts from a category:
Return posts within the category, but exclude sticky posts completely, and adhere to paging rules:
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$sticky = get_option( 'sticky_posts' );
$args = array(
'cat' => 3,
'ignore_sticky_posts' => 1,
'post__not_in' => $sticky,
'paged' => $paged,
);
$query = new WP_Query( $args );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment