Query Posts


query_posts() is the easiest, but not preferred or most efficient, way to alter the default query that WordPress uses to display posts. Use query_posts() to display different posts than those that would normally show up at a specific URL. (The preferred way is hooking into ‘pre get posts’ and altering the main query that way using is main query)

For example, on the homepage, you would normally see the latest 10 posts. If you want to show only 5 posts (and don’t care about pagination), you can use query_posts() like so: query_posts( 'posts_per_page=5' );

Examples:

query_posts( 'cat=1&tag=apples' );

Exclude Categories From Your Home Page

Placing this code in index.php file will cause the home page to display posts from all categories except category ID 3.

<?php
if ( is_home() ) {
	query_posts( 'cat=-3' );
}
?>

You can also add some more categories to the exclude-list (tested with WP 3.3.1):

<?php
if ( is_home() ) {
	query_posts( 'cat=-1,-2,-3' );
}
?>

Retrieve a Particular Post

To retrieve a particular post, you could use the following:

query_posts( 'p=5' );

Note: If the particular post is an attachment, you have to use attachment_id instead of p:

query_posts( 'attachment_id=5' );

If you want to use the Read More functionality with this query, you will need to set the global $more variable to 0.

<?php
// retrieve one post with an ID of 5
query_posts( 'p=5' );

// set $more to 0 in order to only get the first part of the post
global $more;
$more = 0;

// the Loop
while (have_posts()) : the_post();
	the_content( 'Read the full post »' );
endwhile;
?>
Advertisements

WP-Quey – Post & Page Parameters


Display content based on post and page parameters.

  • p (int) – use post id.
  • name (string) – use post slug.
  • page_id (int) – use page id.
  • pagename (string) – use page slug.
  • post_parent (int) – use page id. Return just the child Pages.
  • post__in (array) – use post ids. Specify posts to retrieve.
  • post__not_in (array) – use post ids. Specify post NOT to retrieve.

Show Post/Page by ID

Display post by ID:

$query = new WP_Query( 'p=7' );

Display page by ID:

$query = new WP_Query( 'page_id=7' );

Show Post/Page by Slug

Display post by slug:

$query = new WP_Query( 'name=about-my-life' );

Display page by slug:

$query = new WP_Query( 'pagename=contact' );

Show Child Posts/Pages

Display child page using the slug of the parent and the child page, separated by a slash (e.g. ‘parent_slug/child_slug’):

$query = new WP_Query( 'pagename=contact_us/canada' );

Display child pages using parent page ID:

$query = new WP_Query( 'post_parent=93' );

Display only top-level pages, exclude all child pages:

$query = new WP_Query( 'post_parent=0' );

Multiple Posts/Pages Handling

Display only the specific posts:

$query = new WP_Query( array( 'post_type' => 'page', 'post__in' => array( 2, 5, 12, 14, 20 ) ) );

Display all posts but NOT the specified ones:

$query = new WP_Query( array( 'post_type' => 'post', 'post__not_in' => array( 2, 5, 12, 14, 20 ) ) );

Note: you cannot combine ‘post__in’ and ‘post__not_in’ in the same query.

WP Query – Category Parameters


  • cat (int) – use category id.
  • category_name (string) – use category slug (NOT name).
  • category__and (array) – use category id.
  • category__in (array) – use category id.
  • category__not_in (array) – use category id.

Show Posts for One Category:

Display posts that have this category (and any children of that category), using category id:

$query = new WP_Query( 'cat=4' );

Display posts that have this category (and any children of that category), using category slug:

$query = new WP_Query( 'category_name=staff' );

Display posts that have this category (not children of that category), using category id:

$query = new WP_Query( 'category__in=4' );

Show Posts From Several Categories

Display posts that have these categories, using category id:

$query = new WP_Query( 'cat=2,6,17,38' );

Display posts that have these categories, using category slug:

$query = new WP_Query( 'category_name=staff,news' );

Exclude Posts Belonging to Category

Display all posts except those from a category by prefixing its id with a ‘-‘ (minus) sign.

$query = new WP_Query( 'cat=-12,-34,-56' );

Multiple Category Handling

Display posts that are in multiple categories. This shows posts that are in both categories 2 and 6:

$query = new WP_Query( array( 'category__and' => array( 2, 6 ) ) );

To display posts from either category 2 OR 6, you could use cat as mentioned above, or by using category__in (note this does not show posts from any children of these categories):

$query = new WP_Query( array( 'category__in' => array( 2, 6 ) ) );

You can also exclude multiple categories this way:

$query = new WP_Query( array( 'category__not_in' => array( 2, 6 ) ) );

WP Query in wordpress


Show Posts for one Author

Display posts by author, using author id:

$query = new WP_Query( 'author=123' );

Display posts by author, using author ‘user_nicename’:

$query = new WP_Query( 'author_name=rami' );

Show Posts From Several Authors

Display posts from several specific authors:

$query = new WP_Query( 'author=2,6,17,38' );

Exclude Posts Belonging to an Author

Display all posts except those from an author(singular) by prefixing its id with a ‘-‘ (minus) sign:

$query = new WP_Query( 'author=-12' );