フリーランス|WEB 制作経験丸7年、制作会社様からのご依頼に丁寧・高クオリティでお手伝いいたします。 IRODORI DESIGN

B L O G

【WordPress】記事一覧から非公開記事を除外する方法

pre_get_posts
WordPress
非公開

ある時、クライアントから非公開にしている記事が表示されているから表示されないようにしてほしい!と連絡がありました。
記事一覧を表示する際、非公開記事は基本的に除外されますが、WordPress管理画面にログイン中の時は非公開記事も含まれて表示されます。

記事一覧から非公開記事を除外する方法

非公開記事を除外する場合、メインループとサブループでやり方が変わってきます。

メインループの場合

メインループの場合はpre_get_postsフックを使います。
pre_get_postsは、WordPressのアクションフックのひとつで、メインループを操作するためのものです。
実際にはfunctions.phpに以下の記述をします。

function my_pre_get_posts($query) {
    if( is_admin() || $query->is_main_query() ) {
        return;
    }
    if( is_archive() ) {
        $query->set('post_status','publish');
    }
}
add_action('pre_get_posts','my_pre_get_posts');

これでメインループで表示する記事一覧は公開済みの記事だけに絞られ、非公開記事は除外することが出来ます。

特定のカスタム投稿タイプだけ実装したい場合は、functiosn.phpに以下の記述をします。

function my_pre_get_posts($query) {
    if( is_admin() || $query->is_main_query() ) {
        return;
    }
    if( is_post_type_archive('●●●') ) {
        $query->set('post_status','publish');
    }
}
add_action('pre_get_posts','my_pre_get_posts');

「●●●」の部分には非公開記事を除外したいカスタム投稿タイプ名を指定します。

サブループの場合

サブループの場合は、直接クエリに公開済みの記事だけを取得する指定をします。

$args = array(
    'post_type' => '●●●',
    'post_status' => 'publish',
);

「●●●」の部分には非公開記事を除外したいカスタム投稿タイプ名を指定します。
標準の投稿の場合は「post」を指定します。

その他のステータスの場合

記事は公開済みの他にも様々なステータスがあります。その他のステータスで取得する記事を絞込む場合は、以下のようになります。

「予約済み」の記事を取得する場合

「予約済み」の記事を取得する場合は、「future」を指定します。

メインループの場合

function my_pre_get_posts($query) {
    if( is_admin() || $query->is_main_query() ) {
        return;
    }
    if( is_post_type_archive('●●●') ) {
        $query->set('post_status','future');
    }
}
add_action('pre_get_posts','my_pre_get_posts');

サブループの場合

$args = array(
    'post_type' => '●●●',
    'post_status' => 'future',
);

「下書き」の記事を取得する場合

「下書き」の記事を取得する場合は、「draft」を指定します。

メインループの場合

function my_pre_get_posts($query) {
    if( is_admin() || $query->is_main_query() ) {
        return;
    }
    if( is_post_type_archive('●●●') ) {
        $query->set('post_status','draft');
    }
}
add_action('pre_get_posts','my_pre_get_posts');

サブループの場合

$args = array(
    'post_type' => '●●●',
    'post_status' => 'draft',
);

「承認待ち」の記事を取得する場合

「承認待ち」の記事を取得する場合は、「pending」を指定します。

メインループの場合

function my_pre_get_posts($query) {
    if( is_admin() || $query->is_main_query() ) {
        return;
    }
    if( is_post_type_archive('●●●') ) {
        $query->set('post_status','pending');
    }
}
add_action('pre_get_posts','my_pre_get_posts');

サブループの場合

$args = array(
    'post_type' => '●●●',
    'post_status' => 'pending',
);

「非公開」の記事を取得する場合

「非公開」の記事を取得する場合は、「private」を指定します。

メインループの場合

function my_pre_get_posts($query) {
    if( is_admin() || $query->is_main_query() ) {
        return;
    }
    if( is_post_type_archive('●●●') ) {
        $query->set('post_status','private');
    }
}
add_action('pre_get_posts','my_pre_get_posts');

サブループの場合

$args = array(
    'post_type' => '●●●',
    'post_status' => 'private',
);

「ゴミ箱」の記事を取得する場合

「ゴミ箱」の記事を取得する場合は、「trash」を指定します。

メインループの場合

function my_pre_get_posts($query) {
    if( is_admin() || $query->is_main_query() ) {
        return;
    }
    if( is_post_type_archive('●●●') ) {
        $query->set('post_status','trash');
    }
}
add_action('pre_get_posts','my_pre_get_posts');

サブループの場合

$args = array(
    'post_type' => '●●●',
    'post_status' => 'trash',
);

「自動保存」の記事を取得する場合

「自動保存」の記事を取得する場合は、「auto-draft」を指定します。

メインループの場合

function my_pre_get_posts($query) {
    if( is_admin() || $query->is_main_query() ) {
        return;
    }
    if( is_post_type_archive('●●●') ) {
        $query->set('post_status','auto-draft');
    }
}
add_action('pre_get_posts','my_pre_get_posts');

サブループの場合

$args = array(
    'post_type' => '●●●',
    'post_status' => 'auto-draft',
);

「継承」の記事を取得する場合

「継承」の記事を取得する場合は、「inherit」を指定します。

メインループの場合

function my_pre_get_posts($query) {
    if( is_admin() || $query->is_main_query() ) {
        return;
    }
    if( is_post_type_archive('●●●') ) {
        $query->set('post_status','inherit');
    }
}
add_action('pre_get_posts','my_pre_get_posts');

サブループの場合

$args = array(
    'post_type' => '●●●',
    'post_status' => 'inherit',
);

まとめ

いかがでしたか?
記事にはそれぞれ公開ステータスが設定されていています。
状況に応じて取得する記事を適切にコントロールしていきたいですね。