is_main_query

Sponsored Link

この関数は、pre_get_postsにフックを有効にし、メインクエリーのみ修正します。これはboolean関数で、TRUEまたはFALSEのいずれかを返すことを意味します。注意:管理画面はメインクエリーが存在しており、この関数はそこにそれを検出するために用いることができます。

Sponsored Link

This function enables someone to hook into pre_get_posts and modify only the main query. This is a boolean function, meaning it returns either TRUE or FALSE. NOTE: admin screens also have a main query and this function can be used to detect it there.

is_main_queryのファンクションタグ使用方法

<?php is_main_query(); ?>

is_main_queryのパラメーター

このタグには、任意のパラメータを受け付けません。

This tag does not accept any parameters.

返り値

(boolean)

成功した場合にtrue、失敗した場合にはfalseを返します。

True on success, false on failure.

add_action( 'pre_get_posts', 'foo_modify_query_exclude_category' );
function foo_modify_query_exclude_category( $query ) {
    if ( ! is_admin() &amp;&amp; $query->is_main_query() &amp;&amp; ! $query->get( 'cat' ) )
        $query->set( 'cat', '-5' );
}

変更ログ

Since: 3.3

ソースファイル

is_main_query()は、wp-includes/query.php内に位置しています。

is_main_query() is located in wp-includes/query.php.

関連ファンクションタグ

  • Class: WP_Query – Detailed overview of the WP_Query class
  • Class: $wpdb – Overview of using the $wpdb object
  • Function: get_query_var()
  • Function: query_posts() – Make additional custom queries
  • Function: get_posts() – A specialized function that returns an array of posts
  • Function: get_pages() – A specialized function that returns an array of pages
  • Function: have posts() – A conditional that determines if the query returned any posts
  • Function: the_post() – Used to automatically set up the loop after a query
  • Function: rewind_posts() – Resets the current loop
  • Function: setup_postdata() – Setup query data for individual results within a loop
  • Function: wp_reset_postdata() – Restores the previous query (usually after a loop-within-a-loop)
  • Function: wp_reset_query()
  • Function: is_main_query() – Ensures the query being changed is only the main query
  • Action Hook: pre_get_posts – Modify WordPress queries before they are executed
  • Filter Hook: found_posts – Modify the WP_Query object’s found_posts value
  • Tutorial: Displaying Posts Using a Custom Select Query
  • Tutorial: Making Advanced Taxonomy Queries
  • Tutorial: Making Custom Queries using Offset and Pagination
Sponsored Link