/

October 25, 2011

WordPress Query Post

If you have to display a fixed post in your wordpress template, then you can use below code to show the respective post to your page. The ID below allows you to choose the respective post.


Few other Methods and Properties

This is the formal documentation of WP_Query. You shouldn’t alter the properties directly, but instead use the methods to interact with them. Also see Interacting with WP_Query for some useful functions that avoid the need to mess around with class internals and global variables.

Properties

$query
Holds the query string that was passed to the $wp_query object by WP class.
$query_vars
An associative array containing the dissected $query: an array of the query variables and their respective values.
$queried_object
Applicable if the request is a category, author, permalink or Page. Holds information on the requested category, author, post or Page.
$queried_object_id
If the request is a category, author, permalink or page, holds the corresponding ID.
$posts
Gets filled with the requested posts from the database.
$post_count
The number of posts being displayed.
$found_posts
The total number of posts found matching the current query parameters
$max_num_pages
The total number of pages. Is the result of $found_posts / $posts_per_page
$current_post
(available during The Loop) Index of the post currently being displayed.
$post
(available during The Loop) The post currently being displayed.
$is_single, $is_page, $is_archive, $is_preview, $is_date, $is_year, $is_month, $is_time, $is_author, $is_category, $is_tag, $is_tax, $is_search, $is_feed, $is_comment_feed, $is_trackback, $is_home, $is_404, $is_comments_popup, $is_admin, $is_attachment, $is_singular, $is_robots, $is_posts_page, $is_paged
Booleans dictating what type of request this is. For example, the first three represent ‘is it a permalink?’, ‘is it a Page?’, ‘is it any type of archive page?’, respectively.

Methods

(An ampersand (&) before a method name indicates it returns by reference.)

init()
Initialise the object, set all properties to null, zero or false.
parse_query( $query )
Takes a query string defining the request, parses it and populates all properties apart from $posts, $post_count, $post and $current_post.
parse_query_vars()
Reparse the old query string.
get( $query_var )
Get a named query variable.
set( $query_var, $value )
Set a named query variable to a specific value.
&get_posts()
Fetch and return the requested posts from the database. Also populate $posts and $post_count.
next_post()
(to be used when in The Loop) Advance onto the next post in $posts. Increment $current_post and set $post to the (new) current post object (note: this does not set the global $post variable, only the WP_Query object’s instance variable.) Returns the current post object
the_post()
(to be used when in The Loop) Advance onto the next post, and set the global $post variable.
have_posts()
(to be used when in The Loop, or just before The Loop) Determine if we have posts remaining to be displayed.
rewind_posts()
Reset $current_post and $post.
&query( $query )
Call parse_query() and get_posts(). Return the results of get_posts().
get_queried_object()
Set $queried_object if it’s not already set and return it.
get_queried_object_id()
Set $queried_object_id if it’s not already set and return it.
WP_Query( $query = '' ) (constructor)
If you provide a query string, call query() with it.