When working with WordPress, it is often necessary to display content based on specific post or page parameters. The WordPress Query class, WP_Query, allows developers to retrieve and display posts and pages based on a variety of 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

One of the most basic ways to display content is by using the post or page ID. The ‘p’ parameter can be used to display a post by its ID, while the ‘page_id’ parameter can be used to display a page by its ID. For example, the following code would display the post with an ID of 7:

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

Another way to display content is by using the post or page slug. The ‘name’ parameter can be used to display a post by its slug, while the ‘pagename’ parameter can be used to display a page by its slug. For example, the following code would display a post with the slug ‘about-my-life’:

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:

In addition to displaying content by ID or slug, it is also possible to display child posts and pages using the ‘post_parent’ parameter. This parameter can be used with a page ID to display only child pages, or with a post ID to display only child posts. For example, the following code would display child pages of the page with an ID of 93:

$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

Another way to display content is by using the ‘post__in’ and ‘post__not_in’ parameters. The ‘post__in’ parameter allows you to specify an array of post IDs to retrieve, while the ‘post__not_in’ parameter allows you to specify an array of post IDs to exclude. For example, the following code would display only the pages with IDs 2, 5, 12, 14, and 20:

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 ) ) );

It’s important to note that you cannot combine ‘post__in’ and ‘post__not_in’ in the same query.

In conclusion, WP_Query class in WordPress provides a powerful and flexible way to retrieve and display content based on specific post or page parameters. Understanding how to use these parameters can help you create dynamic and customized WordPress sites with ease.