| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Source view] [Print] [Project Stats]
WordPress Query API The query API attempts to get which part of WordPress the user is on. It also provides functionality for getting URL query information.
| File Size: | 1973 lines (62 kb) |
| Included or required: | 0 times |
| Referenced: | 0 times |
| Includes or requires: | 0 files |
| get_query_var( $query_var, $default_value = '' ) X-Ref |
| Retrieves the value of a query variable in the WP_Query class. param: string $query_var The variable key to retrieve. param: mixed $default_value Optional. Value to return if the query variable is not set. return: mixed Contents of the query variable. |
| get_queried_object() X-Ref |
| Retrieves the currently queried object. Wrapper for WP_Query::get_queried_object(). return: WP_Term|WP_Post_Type|WP_Post|WP_User|null The queried object. |
| get_queried_object_id() X-Ref |
| Retrieves the ID of the currently queried object. Wrapper for WP_Query::get_queried_object_id(). return: int ID of the queried object. |
| set_query_var( $query_var, $value ) X-Ref |
| Sets the value of a query variable in the WP_Query class. param: string $query_var Query variable key. param: mixed $value Query variable value. |
| query_posts( $query ) X-Ref |
| Sets up The Loop with query parameters. Note: This function will completely override the main query and isn't intended for use by plugins or themes. Its overly-simplistic approach to modifying the main query can be problematic and should be avoided wherever possible. In most cases, there are better, more performant options for modifying the main query such as via the {@see 'pre_get_posts'} action within WP_Query. This must not be used within the WordPress Loop. param: array|string $query Array or string of WP_Query arguments. return: WP_Post[]|int[] Array of post objects or post IDs. |
| wp_reset_query() X-Ref |
| Destroys the previous query and sets up a new query. This should be used after query_posts() and before another query_posts(). This will remove obscure bugs that occur when the previous WP_Query object is not destroyed properly before another is set up. |
| wp_reset_postdata() X-Ref |
| After looping through a separate query, this function restores the $post global to the current post in the main query. |
| is_archive() X-Ref |
| Determines whether the query is for an existing archive page. Archive pages include category, tag, author, date, custom post type, and custom taxonomy based archives. For more information on this and similar theme functions, check out the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ Conditional Tags} article in the Theme Developer Handbook. return: bool Whether the query is for an existing archive page. |
| is_post_type_archive( $post_types = '' ) X-Ref |
| Determines whether the query is for an existing post type archive page. For more information on this and similar theme functions, check out the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ Conditional Tags} article in the Theme Developer Handbook. param: string|string[] $post_types Optional. Post type or array of posts types return: bool Whether the query is for an existing post type archive page. |
| is_attachment( $attachment = '' ) X-Ref |
| Determines whether the query is for an existing attachment page. For more information on this and similar theme functions, check out the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ Conditional Tags} article in the Theme Developer Handbook. param: int|string|int[]|string[] $attachment Optional. Attachment ID, title, slug, or array of such return: bool Whether the query is for an existing attachment page. |
| is_author( $author = '' ) X-Ref |
| Determines whether the query is for an existing author archive page. If the $author parameter is specified, this function will additionally check if the query is for one of the authors specified. For more information on this and similar theme functions, check out the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ Conditional Tags} article in the Theme Developer Handbook. param: int|string|int[]|string[] $author Optional. User ID, nickname, nicename, or array of such return: bool Whether the query is for an existing author archive page. |
| is_category( $category = '' ) X-Ref |
| Determines whether the query is for an existing category archive page. If the $category parameter is specified, this function will additionally check if the query is for one of the categories specified. For more information on this and similar theme functions, check out the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ Conditional Tags} article in the Theme Developer Handbook. param: int|string|int[]|string[] $category Optional. Category ID, name, slug, or array of such return: bool Whether the query is for an existing category archive page. |
| is_tag( $tag = '' ) X-Ref |
| Determines whether the query is for an existing tag archive page. If the $tag parameter is specified, this function will additionally check if the query is for one of the tags specified. For more information on this and similar theme functions, check out the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ Conditional Tags} article in the Theme Developer Handbook. param: int|string|int[]|string[] $tag Optional. Tag ID, name, slug, or array of such return: bool Whether the query is for an existing tag archive page. |
| is_tax( $taxonomy = '', $term = '' ) X-Ref |
| Determines whether the query is for an existing custom taxonomy archive page. If the $taxonomy parameter is specified, this function will additionally check if the query is for that specific $taxonomy. If the $term parameter is specified in addition to the $taxonomy parameter, this function will additionally check if the query is for one of the terms specified. For more information on this and similar theme functions, check out the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ Conditional Tags} article in the Theme Developer Handbook. param: string|string[] $taxonomy Optional. Taxonomy slug or slugs to check against. param: int|string|int[]|string[] $term Optional. Term ID, name, slug, or array of such return: bool Whether the query is for an existing custom taxonomy archive page. |
| is_date() X-Ref |
| Determines whether the query is for an existing date archive. For more information on this and similar theme functions, check out the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ Conditional Tags} article in the Theme Developer Handbook. return: bool Whether the query is for an existing date archive. |
| is_day() X-Ref |
| Determines whether the query is for an existing day archive. A conditional check to test whether the page is a date-based archive page displaying posts for the current day. For more information on this and similar theme functions, check out the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ Conditional Tags} article in the Theme Developer Handbook. return: bool Whether the query is for an existing day archive. |
| is_feed( $feeds = '' ) X-Ref |
| Determines whether the query is for a feed. For more information on this and similar theme functions, check out the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ Conditional Tags} article in the Theme Developer Handbook. param: string|string[] $feeds Optional. Feed type or array of feed types return: bool Whether the query is for a feed. |
| is_comment_feed() X-Ref |
| Is the query for a comments feed? return: bool Whether the query is for a comments feed. |
| is_front_page() X-Ref |
| Determines whether the query is for the front page of the site. This is for what is displayed at your site's main URL. Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'. If you set a static page for the front page of your site, this function will return true when viewing that page. Otherwise the same as {@see is_home()}. For more information on this and similar theme functions, check out the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ Conditional Tags} article in the Theme Developer Handbook. return: bool Whether the query is for the front page of the site. |
| is_home() X-Ref |
| Determines whether the query is for the blog homepage. The blog homepage is the page that shows the time-based blog content of the site. is_home() is dependent on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'. If a static page is set for the front page of the site, this function will return true only on the page you set as the "Posts page". For more information on this and similar theme functions, check out the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ Conditional Tags} article in the Theme Developer Handbook. return: bool Whether the query is for the blog homepage. |
| is_privacy_policy() X-Ref |
| Determines whether the query is for the Privacy Policy page. The Privacy Policy page is the page that shows the Privacy Policy content of the site. is_privacy_policy() is dependent on the site's "Change your Privacy Policy page" Privacy Settings 'wp_page_for_privacy_policy'. This function will return true only on the page you set as the "Privacy Policy page". For more information on this and similar theme functions, check out the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ Conditional Tags} article in the Theme Developer Handbook. return: bool Whether the query is for the Privacy Policy page. |
| is_month() X-Ref |
| Determines whether the query is for an existing month archive. For more information on this and similar theme functions, check out the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ Conditional Tags} article in the Theme Developer Handbook. return: bool Whether the query is for an existing month archive. |
| is_page( $page = '' ) X-Ref |
| Determines whether the query is for an existing single page. If the $page parameter is specified, this function will additionally check if the query is for one of the pages specified. For more information on this and similar theme functions, check out the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ Conditional Tags} article in the Theme Developer Handbook. param: int|string|int[]|string[] $page Optional. Page ID, title, slug, or array of such return: bool Whether the query is for an existing single page. |
| is_paged() X-Ref |
| Determines whether the query is for a paged result and not for the first page. For more information on this and similar theme functions, check out the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ Conditional Tags} article in the Theme Developer Handbook. return: bool Whether the query is for a paged result. |
| is_preview() X-Ref |
| Determines whether the query is for a post or page preview. For more information on this and similar theme functions, check out the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ Conditional Tags} article in the Theme Developer Handbook. return: bool Whether the query is for a post or page preview. |
| is_robots() X-Ref |
| Is the query for the robots.txt file? return: bool Whether the query is for the robots.txt file. |
| is_favicon() X-Ref |
| Is the query for the favicon.ico file? return: bool Whether the query is for the favicon.ico file. |
| is_sitemap() X-Ref |
| Is the query for a sitemap? return: bool Whether the query is for a sitemap. |
| is_random() X-Ref |
| Determines whether the query is for random content. True for a blog home or archive request that includes the `random` query variable, for example `example.com/random/` or `example.com/random/category/news/`. Search, feed, trackback, robots.txt, favicon and sitemap requests are never for random content. return: bool Whether the query is for random content. |
| is_search() X-Ref |
| Determines whether the query is for a search. For more information on this and similar theme functions, check out the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ Conditional Tags} article in the Theme Developer Handbook. return: bool Whether the query is for a search. |
| is_single( $post = '' ) X-Ref |
| Determines whether the query is for an existing single post. Works for any post type, except attachments and pages If the $post parameter is specified, this function will additionally check if the query is for one of the Posts specified. For more information on this and similar theme functions, check out the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ Conditional Tags} article in the Theme Developer Handbook. param: int|string|int[]|string[] $post Optional. Post ID, title, slug, or array of such return: bool Whether the query is for an existing single post. |
| is_singular( $post_types = '' ) X-Ref |
| Determines whether the query is for an existing single post of any post type (post, attachment, page, custom post types). If the $post_types parameter is specified, this function will additionally check if the query is for one of the Posts Types specified. For more information on this and similar theme functions, check out the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ Conditional Tags} article in the Theme Developer Handbook. param: string|string[] $post_types Optional. Post type or array of post types return: bool Whether the query is for an existing single post |
| is_time() X-Ref |
| Determines whether the query is for a specific time. For more information on this and similar theme functions, check out the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ Conditional Tags} article in the Theme Developer Handbook. return: bool Whether the query is for a specific time. |
| is_trackback() X-Ref |
| Determines whether the query is for a trackback endpoint call. For more information on this and similar theme functions, check out the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ Conditional Tags} article in the Theme Developer Handbook. return: bool Whether the query is for a trackback endpoint call. |
| is_year() X-Ref |
| Determines whether the query is for an existing year archive. For more information on this and similar theme functions, check out the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ Conditional Tags} article in the Theme Developer Handbook. return: bool Whether the query is for an existing year archive. |
| is_404() X-Ref |
| Determines whether the query has resulted in a 404 (returns no results). For more information on this and similar theme functions, check out the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ Conditional Tags} article in the Theme Developer Handbook. return: bool Whether the query is a 404 error. |
| is_embed() X-Ref |
| Is the query for an embedded post? return: bool Whether the query is for an embedded post. |
| is_main_query() X-Ref |
| Determines whether the query is the main query. For more information on this and similar theme functions, check out the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ Conditional Tags} article in the Theme Developer Handbook. return: bool Whether the query is the main query. |
| have_posts() X-Ref |
| Determines whether current WordPress query has posts to loop over. return: bool True if posts are available, false if end of the loop. |
| in_the_loop() X-Ref |
| Determines whether the caller is in the Loop. For more information on this and similar theme functions, check out the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ Conditional Tags} article in the Theme Developer Handbook. return: bool True if caller is within loop, false if loop hasn't started or ended. |
| rewind_posts() X-Ref |
| Rewind the loop posts. |
| the_post() X-Ref |
| Iterate the post index in the loop. |
| have_comments() X-Ref |
| Determines whether current WordPress query has comments to loop over. return: bool True if comments are available, false if no more comments. |
| the_comment() X-Ref |
| Iterate comment index in the comment loop. |
| wp_old_slug_redirect() X-Ref |
| Redirect old slugs to the correct permalink. Attempts to find the current slug from the past slugs. |
| _find_post_by_old_slug( $post_type ) X-Ref |
| Find the post ID for redirecting an old slug. param: string $post_type The current post type based on the query vars. return: int The Post ID. |
| _find_post_by_old_date( $post_type ) X-Ref |
| Find the post ID for redirecting an old date. param: string $post_type The current post type based on the query vars. return: int The Post ID. |
| wp_is_random_content_redirect_enabled() X-Ref |
| Determines whether random content redirects are enabled. return: bool True if random content redirects are enabled, false otherwise. |
| wp_is_random_content_query( WP_Query $query ) X-Ref |
| Determines whether a query is a request for random content. A random content request is a front end, main query for the blog home or an archive that includes the `random` query variable and at least one `randomable` post type. For example `example.com/random/` or `example.com/random/category/news/`. Singular (including embeds), search, feed, trackback, robots.txt, favicon and sitemap requests are never random content requests, nor are requests using an HTTP method other than GET or HEAD. Search requests are excluded as selecting a random post from search results would run an uncacheable search query that orders every matching post randomly. Search requests that include the `random` query variable are redirected to the search results by {@see WP::handle_random()}. param: WP_Query $query The query to check. return: bool True if the query is a request for random content, false otherwise. |
| wp_get_random_content_post_types( WP_Query $query ) X-Ref |
| Retrieves the post types eligible for selection by a random content request. These are the post types the query would otherwise return, limited to those registered as `randomable`. param: WP_Query $query The query to retrieve the post types for. return: string[] Post type names eligible for random selection. |
| wp_random_content_pre_get_posts( $query ) X-Ref |
| Limits the main query to posts eligible for random content requests. The main query is used to select the random post so the request runs the same query, and the same filters, as the archive it is randomizing. As such, callbacks on the {@see 'pre_get_posts'} action may modify the posts eligible for selection, for example to limit the post types or exclude a category: add_action( 'pre_get_posts', static function ( WP_Query $query ) { if ( wp_is_random_content_query( $query ) ) { $query->set( 'category__not_in', array( 1 ) ); } } ); Only randomable post types and published posts without a password are eligible. The number and order of posts are enforced by {@see wp_random_content_post_limits()} and {@see wp_random_content_posts_orderby()}, so callbacks modifying them on archives do not affect the selection. The random post IDs the post is selected from are stored in the `random_sample_ids` query variable, see {@see wp_get_random_content_sample_ids()}. Added to the {@see 'pre_get_posts'} action by {@see WP::handle_random()} while the random content query runs, so it runs after other callbacks. param: WP_Query $query The WP_Query instance (passed by reference). |
| wp_get_random_content_sample_ids() X-Ref |
| Retrieves random post IDs for the random content query to select a post from. Sorting posts randomly requires the database to read every eligible post. Instead, the random content query looks up 50 random post IDs, each by the primary key, and selects one of those that is an eligible post. The cost of the lookup does not depend on the number of posts, and as every post ID is equally likely to be looked up, every eligible post is equally likely to be selected. This is the case whether or not there are gaps between the IDs of posts. The IDs are between 1 and the highest post ID, which is stored in the object cache for 10 minutes. If there are no more than 50 post IDs, all of them are returned. return: int[] Up to 50 distinct post IDs. Posts with the IDs may not exist. |
| _wp_random_content_sample_clause( array $post_ids ) X-Ref |
| Retrieves the SQL clause limiting the random content query to random post IDs. param: int[] $post_ids Post IDs. return: string SQL clause. |
| wp_random_content_posts_where( $where, WP_Query $query ) X-Ref |
| Limits the random content query to the random post IDs. Added to the {@see 'posts_where'} filter by {@see WP::handle_random()} while the random content query runs. param: string $where The WHERE clause of the query. param: WP_Query $query The WP_Query instance. return: string The WHERE clause of the query. |
| wp_random_content_posts_orderby( $orderby, WP_Query $query ) X-Ref |
| Removes the order of the random content query. The random content query is limited to random post IDs, one of which is selected once the query has run, see {@see wp_random_content_posts_pre_query()}. Ordering the posts would require the database to sort them. Added to the {@see 'posts_orderby'} filter by {@see WP::handle_random()} while the random content query runs. Enforcing the order in SQL prevents {@see 'pre_get_posts'} callbacks setting the order of posts on archives from affecting the selection. param: string $orderby The ORDER BY clause of the query. param: WP_Query $query The WP_Query instance. return: string The ORDER BY clause of the query. |
| wp_random_content_post_limits( $limits, WP_Query $query ) X-Ref |
| Removes the limit of the random content query. The random content query is limited to random post IDs, one of which is selected once the query has run, see {@see wp_random_content_posts_pre_query()}. Added to the {@see 'post_limits'} filter by {@see WP::handle_random()} while the random content query runs. Enforcing the limit in SQL prevents {@see 'pre_get_posts'} callbacks setting the number of posts, offset or page on archives from affecting the selection. param: string $limits The LIMIT clause of the query. param: WP_Query $query The WP_Query instance. return: string The LIMIT clause of the query. |
| wp_random_content_posts_fields( $fields, WP_Query $query ) X-Ref |
| Limits the random content query to selecting post IDs. Added to the {@see 'posts_fields'} filter by {@see WP::handle_random()} while the random content query runs. param: string $fields The SELECT clause of the query. param: WP_Query $query The WP_Query instance. return: string The SELECT clause of the query. |
| wp_random_content_posts_pre_query( $posts, WP_Query $query ) X-Ref |
| Selects the random post from the eligible posts among random post IDs. The random content query is limited to 50 random post IDs, see {@see wp_get_random_content_sample_ids()}, and one of the posts it selects is chosen at random. If none of the IDs are eligible posts, the query is run again with other random post IDs, up to three times in total. On sites where most post IDs are eligible posts, a post is nearly always selected by the first query. If no post is selected, eligible posts are too sparse among the post IDs to be found this way, for example a small category on a site with many posts. A pool of up to 1,000 randomly ordered eligible posts is then selected, which is the whole archive if it has no more posts than that, and their IDs are stored in the object cache for 10 minutes. While the pool is cached, the post is selected from it without querying the database for the eligible posts. Selecting the pool requires the database to read every eligible post, so sites with a persistent object cache do so at most once every 10 minutes for each such archive. The pool is selected without excluding password protected posts, which are skipped when the post is selected from it, so the database can read the eligible posts from an index rather than reading each post in full. Only one request selects the pool at a time. Once the pool is 10 minutes old, the first request to need it selects the next pool and, until it has, other requests select from the previous pool, which is kept for an hour. If there is no previous pool, as the archive has not been requested recently, concurrent requests each select a pool. The pool is keyed by the SQL of the random content query, excluding the random post IDs, so each archive, and each variation of an archive produced by {@see 'pre_get_posts'} callbacks or query filters, has its own pool. As the pool may be up to 10 minutes old, posts that have since been unpublished, deleted, password protected or changed to another post type are skipped. If none of its posts are eligible, a post is selected as if there was no pool. Other changes, such as a post being published or removed from a term, are reflected once the pool expires. This includes the empty pool of an archive without eligible posts, which is cached so the archive is not queried for every request. Added to the {@see 'posts_pre_query'} filter by {@see WP::handle_random()} while the random content query runs. The database is only read from, never written to. param: WP_Post[]|int[]|null $posts Posts short-circuiting the query, or null to run the query. param: WP_Query $query The WP_Query instance. return: WP_Post[]|int[]|null The selected post, an empty array if no post is eligible, |
| wp_random_content_redirect() X-Ref |
| Redirects random content requests to the randomly selected post. Added to the {@see 'template_redirect'} action at priority 100 by {@see WP::handle_random()} once the main query has selected the random post, so the redirect is sent after the request is set up and the {@see 'wp'} action has fired. The headers preventing the redirect being cached or indexed are sent with it. The post is read from the main query rather than the current query, so the redirect is still sent when an earlier callback replaces the current query, for example by calling {@see query_posts()}. The request ends once a post is selected. The redirect is to the post's permalink as returned by {@see get_permalink()}, including a permalink filtered to be on another site. If no post is selected, for example on an empty archive, {@see WP::handle_random()} handles the request as a 404. |
| wp_random_content_flush_rewrite_rules_for_post( $post_id, $post = null, $update = false, $post_before = null ) X-Ref |
| Flushes the rewrite rules when a page or post at the random content base changes. A published page at the random base, for example `example.com/random/`, takes precedence over random content redirects. So does a published post when the permalink structure places posts at the same URL, for example `/%postname%/`, see {@see WP_Rewrite::post_permalinks_share_random_base()}. As the rewrite rules account for the page or post, they need to be regenerated when it is published, unpublished, renamed, moved or deleted. Runs on the {@see 'wp_after_insert_post'} and {@see 'after_delete_post'} actions. Third-party code may fire these actions with unexpected values, so the parameters are not type declared. When `$post` is omitted it is looked up from `$post_id`. Passing anything other than a `WP_Post` as `$post`, or a post that cannot be looked up, is incorrect usage and nothing is flushed. `$post_before` is only considered on `wp_after_insert_post`, where passing anything other than a `WP_Post` or null is incorrect usage and the value is ignored. param: int $post_id Post ID. param: WP_Post|null $post Optional. Post object. Looked up from `$post_id` when null. Default null. param: bool $update Optional. Whether this is an existing post being updated. Default false. param: WP_Post|null $post_before Optional. Post object before the update, null for new posts. Default null. |
| setup_postdata( $post ) X-Ref |
| Set up global post data. param: WP_Post|object|int $post WP_Post instance or Post ID/object. return: bool True when finished. |
| generate_postdata( $post ) X-Ref |
| Generates post data. param: WP_Post|object|int $post WP_Post instance or Post ID/object. return: array|false Elements of post, or false on failure. |
| Generated : Sun Nov 29 08:20:01 2026 | Cross-referenced by PHPXref |