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.

Advertisements