web-dev-qa-db-ja.com

ネストループでポストデータを前のループにリセットする

私はポストプラグインへのポストでネストループを使用しようとしています。どちらのループも機能しますが、2番目のネストしたループの後に問題が発生します($ issue)。再度$ publicationループにアクセスしたいのですが、データは$ issueデータのままです。

wp_reset_query()はsingle.phpのメインループに戻ります。

新しいWP_Queryの代わりにget_posts()を使用できますが、get_template_part()を使用できるようにしたいです。

2番目の「出版物のタイトル」が出版物であって号ではなく出版物を返すように、データを出版物ループに戻すにはどうすればよいですか

これがsingle.php内の私のコードです。

$publication = new WP_Query( array(
'connected_type'  => 'publication_to_post',
'connected_items' => $post->ID,
'fields'          => 'ids',
'posts_per_page'  => 1,
) );

if ( $publication->have_posts() ) {
while ( $publication->have_posts() ) : $publication->the_post();
    echo '<h2>Publication title = '.get_the_title().'</h2>';
    $pub_id = get_the_ID();

    $issue = new WP_Query( array(
        'connected_type'  => 'publication_to_issue',
        'connected_items' => $pub_id,
        'fields'          => 'ids',
        'posts_per_page'  => 1,
    ) );

    if ( $issue->have_posts() ) {
        while ( $issue->have_posts() ) : $issue->the_post();

            // need to be able to use template parts in here
            echo '<h2>Issue title = '.get_the_title().'</h2>';

        endwhile;
    }

    // This currently returns the issue title, not the publication title
    echo '<h2>Publication title = '.get_the_title().'</h2>';

endwhile;
}
20
kdev

私はこれに自分自身で答えるつもりですが、これを解決したのは非常に賢いCode for the Peopleです。

wp_reset_postdata()またはwp_reset_query()を使用する代わりに、以下を使用できます。

$publication->reset_postdata();

$ publicationはクエリオブジェクトです。

作業コードは次のようになります。

$publication = new WP_Query( array(
'connected_type'  => 'publication_to_post',
'connected_items' => $post->ID,
'fields'          => 'ids',
'posts_per_page'  => 1,
) );

if ( $publication->have_posts() ) {
while ( $publication->have_posts() ) : $publication->the_post();
    echo '<h2>Publication title = '.get_the_title().'</h2>';
    $pub_id = get_the_ID();

    $issue = new WP_Query( array(
        'connected_type'  => 'publication_to_issue',
        'connected_items' => $pub_id,
        'fields'          => 'ids',
        'posts_per_page'  => 1,
    ) );

    if ( $issue->have_posts() ) {
        while ( $issue->have_posts() ) : $issue->the_post();

            // need to be able to use template parts in here
            echo '<h2>Issue title = '.get_the_title().'</h2>';

        endwhile; $publication->reset_postdata();
    }

    echo '<h2>Publication title = '.get_the_title().'</h2>';

endwhile;
}
20
kdev

まず最初に、get_posts()setup_postdata()と組み合わせて使うことが可能だと思います。これらを使えば、通常のWordPressループのようにテンプレートタグを使うことができます。

しかし、この関数は入れ子になったループでも使用できます。

# make sure $post is the global in your scope (which should be the case in single.php)
global $post;
if ( $publication->have_posts() ) {
while ( $publication->have_posts() ) : $publication->the_post();
    echo '<h2>Publication title = '.get_the_title().'</h2>';
    $pub_id = get_the_ID();

    # preserve the current post in the higher loop
    $preserve_post = get_post();

    $issue = new WP_Query( array(
        'connected_type'  => 'publication_to_issue',
        'connected_items' => $pub_id,
        'fields'          => 'ids',
        'posts_per_page'  => 1,
    ) );

    if ( $issue->have_posts() ) {
        while ( $issue->have_posts() ) : $issue->the_post();

            // need to be able to use template parts in here
           echo '<h2>Issue title = '.get_the_title().'</h2>';

        endwhile;
    }

    # set the global back to your first loop post
    $post = $preserve_post;
    setup_postdata( $post );
    // This currently returns the issue title, not the publication title
    echo '<h2>Publication title = '.get_the_title().'</h2>';

endwhile;
}
wp_reset_query();
5
David