• 赤色のリンクは、まだ日本語Codexに存在しないページ・画像です。英語版と併せてご覧ください。(詳細

このWikiはいつでも誰でも編集できます

「プラグイン API/アクションフック一覧/pre get posts」の版間の差分

提供: WordPress Codex 日本語版
移動先: 案内検索
(Usage)
(説明)
3行目: 3行目:
  
  
'''<tt>pre_get_posts</tt>''' アクションにより、開発者は <tt>$query</tt> オブジェクトへのアクセスできます。(<tt>$query</tt> への変更はオリジナルのオブジェクトに直接反映されます - no return value is necessary).
+
'''<tt>pre_get_posts</tt>''' アクションにより、開発者は <tt>$query</tt> オブジェクトへのアクセスできます(<tt>$query</tt> への変更はオリジナルのオブジェクトに直接反映されます - 返り値は必要ありません)
  
 
==書き方==
 
==書き方==

2012年12月23日 (日) 20:46時点における版

説明

このフックはクエリー変数オブジェクトの生成後、実際にクエリーが実行される前に呼び出されます。


pre_get_posts アクションにより、開発者は $query オブジェクトへのアクセスできます($query への変更はオリジナルのオブジェクトに直接反映されます - 返り値は必要ありません)。

書き方

<?php add_action( 'pre_get_posts', 'function_name' ); ?>

"function_name" は呼び出される関数名です。

Notes

  • The $query object passed to your function is passed by reference. You do not need to declare globals or return a value. Any changes you make to the object from inside your function are made to the original immediately.
  • When using pre_get_posts, be aware of the query you are changing. Function_Reference/is_main_query is a good method to ensure you are altering only the primary query.
  • This filter affects admin screen queries also. Be sure to check if your modification is affecting your post edit screens. For example, just checking `is_main_query()` and `is_post_type_archive('custom')` will change your query for the `edit.php?post_type=custom` screen, unless you also check for `! is_admin()`

Example: Exclude categories on your main page

This is how you can exclude categories of posts from displaying in your blog. For example, if you have 2 categories of posts (uncategorized '1' and another '1347') that you don't want to display on your 'home' blog page, you can use the following in your plugin to omit these categories:

function exclude_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'cat', '-1,-1347' );
    }
}
add_action( 'pre_get_posts', 'exclude_category' );

Example: Changing the number of posts per page, by post type

WordPress includes a single global setting for controlling the number of posts that appear one one loop page (under "Blog pages show at most" in the admin"). It is possible to create an action hook that changes / overrides the posts_per_page setting on a case-by-case basis. Best of all, this is done before the query is even executed (so there is no performance cost)!

The following example demonstrates how to override the page size ('posts_per_page') for archives of specific post types:

function hwl_home_pagesize( $query ) {
    if ( is_admin() || ! $query->is_main_query() )
        return;

    if ( is_home() ) {
        // Display only 1 post for the original blog archive
        $query->set( 'posts_per_page', 1 );
        return;
    }

    if ( is_post_type_archive( 'movie' ) ) {
        // Display 50 posts for a custom post type called 'movie'
        $query->set( 'posts_per_page', 50 );
        return;
    }
}
add_action( 'pre_get_posts', 'hwl_home_pagesize', 1 );

Example WP_Query Object

For reference, this is one possible example of the WP_Query object ($query) exposed by this hook. For more detail, you can also review the Class_Reference/WP_Query codex page.

WP_Query Object
(
    [query_vars] => Array
        (
            [page] => 
            [pagename] => blog
            [error] => 
            [m] => 0
            [p] => 0
            [post_parent] => 
            [post_type] =>
            [subpost] => 
            [subpost_id] => 
            [attachment] => 
            [attachment_id] => 0
            [name] => 
            [static] => 
            [page_id] => 0
            [second] => 
            [minute] => 
            [hour] => 
            [day] => 0
            [monthnum] => 0
            [year] => 0
            [w] => 0
            [category_name] => 
            [tag] => 
            [cat] => 
            [tag_id] => 
            [author_name] => 
            [feed] => 
            [tb] => 
            [paged] => 0
            [comments_popup] => 
            [meta_key] => 
            [meta_value] => 
            [preview] => 
            [s] => 
            [sentence] => 
            [fields] => 
            [category__in] => Array
                (
                )

            [category__not_in] => Array
                (
                )

            [category__and] => Array
                (
                )

            [post__in] => Array
                (
                )

            [post__not_in] => Array
                (
                )

            [tag__in] => Array
                (
                )

            [tag__not_in] => Array
                (
                )

            [tag__and] => Array
                (
                )

            [tag_slug__in] => Array
                (
                )

            [tag_slug__and] => Array
                (
                )

        )

    [tax_query] => 
    [meta_query] => 
    [post_count] => 0
    [current_post] => -1
    [in_the_loop] => 
    [comment_count] => 0
    [current_comment] => -1
    [found_posts] => 0
    [max_num_pages] => 0
    [max_num_comment_pages] => 0
    [is_single] => 
    [is_preview] => 
    [is_page] => 
    [is_archive] => 
    [is_date] => 
    [is_year] => 
    [is_month] => 
    [is_day] => 
    [is_time] => 
    [is_author] => 
    [is_category] => 
    [is_tag] => 
    [is_tax] => 
    [is_search] => 
    [is_feed] => 
    [is_comment_feed] => 
    [is_trackback] => 
    [is_home] => 1
    [is_404] => 
    [is_comments_popup] => 
    [is_paged] => 
    [is_admin] => 
    [is_attachment] => 
    [is_singular] => 
    [is_robots] => 
    [is_posts_page] => 1
    [is_post_type_archive] => 
    [query_vars_hash] => 41032f87127fba65fb6743b1e97d8662
    [query_vars_changed] => 
    [thumbnails_cached] => 
    [query] => Array
        (
            [page] => 
            [pagename] => blog
        )

    [queried_object] => stdClass Object
        (
            [ID] => 16
            [post_author] => 1
            [post_date] => 2012-01-31 17:23:57
            [post_date_gmt] => 2012-01-31 17:23:57
            [post_content] => 
            [post_title] => Blog
            [post_excerpt] => 
            [post_status] => publish
            [comment_status] => open
            [ping_status] => open
            [post_password] => 
            [post_name] => blog
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2012-01-31 17:23:57
            [post_modified_gmt] => 2012-01-31 17:23:57
            [post_content_filtered] => 
            [post_parent] => 0
            [guid] => 

Related

このページ「プラグイン API/アクションフック一覧/pre get posts」はまだ書きかけで、情報が不足しています。続きを書いて WordPress を手助けしましょう.


最新英語版: WordPress Codex » pre_get_posts最新版との差分