0

うまくいけば、このシナリオで私を助けることができます:

ショートコード関数内に異なる投稿タイプのクエリがいくつかあります。今、これらのクエリをトランジェントで保存しようとしています。ただし、これらのトランジェントには、ショートコードが呼び出されるページごとに一意の名前が必要です。

$trans_posts_golfcourse_ = 'trans_posts_golfcourse_'.$landingpage;

if( false === ( $$trans_posts_golfcourse_ = get_transient( 'trans_posts_golfcourse_' ) ) ) {

    $args = array (
        'posts_per_page'=> 5,
        'post__in'      => $posts_golfcourse,
        'post_type'     => 'golfcourse',
        'post_status'   => 'publish',
        'cache_results' => false,
    );

    $$trans_posts_golfcourse_ = new WP_Query( $args );

    set_transient( 'trans_posts_golfcourse_', $$trans_posts_golfcourse_, 60*60*4 );
}

動的に生成される変数名は

$$trans_posts_golfcourse_

しかし、これはパラメータとしてどのように見える必要がありますか?:

get_transient( 'trans_posts_golfcourse_' )

前もって感謝します!

編集: パラメータとしての動的変数の解決策が見つかりまし た パラメータ(文字列)は、変数名と同じ方法で生成する必要があります:

 get_transient( 'trans_posts_golfcourse_'.$landingpage )

完全なコード:

$trans_posts_golfcourse_ = 'trans_posts_golfcourse_'.$landingpage;
if( false === ( ${$trans_posts_golfcourse_} = get_transient( 'trans_posts_golfcourse_'.$landingpage ) ) ) {

    $args = array (
        'posts_per_page'=> 5,
        'post__in'      => $posts_golfcourse,
        'post_type'     => 'golfcourse',
        'post_status'   => 'publish',
        'cache_results' => false,
    );

    ${$trans_posts_golfcourse_} = new WP_Query( $args );

    set_transient( 'trans_posts_golfcourse_'.$landingpage, ${$trans_posts_golfcourse_}, 60*60*4 );
}

EDIT:トランジェントは正しく呼び出されているようですが、トランジェントはクエリを減らしていません。誰かアイデアがありますか?

4

1 に答える 1

1

解決策は、クエリループをトランジェント内に配置することです。次の 2 つの方法が可能です。

  1. 関数内 (ショートコード fe)

        function get_content( $dynamic_var ){
            $transient_time = 60*60*4;
            $transient_name = "transient_name_" . $dynamic_var;
    
            $content = get_transient( $transient_key );
            if( !empty($content)  ) { return $content; }
    
            $args = array ('');
            $query = new WP_Query( $args );
    
            $content = '';
            if( $query->have_posts() ):
                while( $query->have_posts() ) : $query->the_post();
                    $content.= 'some_content';
                endwhile; wp_reset_postdata();
            endif;
    
            set_transient( $transient_name, $content, $transient_time );
            return $content;
        }
    
  2. テンプレート内

            $transient_time = 60*60*4;
            $transient_name = 'transient_name_' . $page_id;
            // ${$transient_name} > name of variable is dynamically created by
            // the value of variable $transient_name (search for > php variable variables)
    
            $content = '';
            if( false === ( ${$transient_name} = get_transient( $transient_name ) ) ) {
                $args = array ('');
                $query = new WP_Query( $args );
    
                $content_inner = '';
                if( $query->have_posts() ):
                    while( $query->have_posts() ) : $query->the_post();
                        $content_inner.= 'some_content';
                    endwhile; wp_reset_postdata();
                endif;
    
                set_transient( $content_inner, ${$transient_name}, $transient_time );
            }
            $content.= ${$transient_name};
    
于 2015-07-21T10:44:05.307 に答える