0

hopefully you can help me with this scenario:

I have several queries of different post types inside shortcode function. Now I´m trying to store those queries with transients. But those transients need to have a unique name for each page from where the shortcode is called.

$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 );
}

The dynamically generated variable name is

$$trans_posts_golfcourse_

But how does this have to look like as a parameter?:

get_transient( 'trans_posts_golfcourse_' )

Thanks in advance!

EDIT: found solution for dynamic variable as parameter The parameter(string) has to be generated the same way as the variable name:

 get_transient( 'trans_posts_golfcourse_'.$landingpage )

complete code:

$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: Transient is not reducing queries, although transient seems to be called correctly. Does anybody have an idea?

Funkateer
  • 11
  • 5
  • Add your complete loop and everything inside the transient. Check out [this post](http://wordpress.stackexchange.com/a/160618/31545) I have done on [wordpress.se]. Just note, you should build a string, not an array as in the example as shortcodes can't return arrays, just strings – Pieter Goosen Jul 20 '15 at 09:46
  • Thanks Pieter! That´s absolutely right. I´ve managed it the following way last night: ' $transient_key = 'trans_markers_' . $page_id; $transient = get_transient( $transient_key ); if( ! empty( $transient ) ) { return $transient; } ... do the query ... set_transient( $out, $transient_key, $transient_time ); return $out;' – Funkateer Jul 21 '15 at 09:51
  • You should post that as an answer. Pretty useless in comments as code in comments are totally unreadable. ;-) – Pieter Goosen Jul 21 '15 at 09:56
  • yeah, I´m new to stack as you can see ;) – Funkateer Jul 21 '15 at 09:57

1 Answers1

1

The solution is to put the query and the loop inside a transient. Two methods are possible:

  1. Inside a function (shortcode f.e.)

        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. Inside a template

            $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};
    
Funkateer
  • 11
  • 5
  • +1 for posting a solution, and also for posting two different approaches. Remember to accept your own answer as the correct one. I think you have to wait almost two days to do that, so don't forget ;-) – Pieter Goosen Jul 21 '15 at 11:24