0

I have created a little shortcode that allows a user to output a custom post type contents into another page: [module name="foo"]

On a given page I have:

[module name="foo"]
[module name="bar"]

However what is output is just foo twice.

The relevant code:

<?php 

// ASSUMING [module name="some-slug"]
function cpt_shortcode( $atts ){
    $a = shortcode_atts( array(
        'name' => 'something else',
    ), $atts );

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

        $shortcode_post = new WP_Query( array(
            'post_type'     => 'module',
            'name'          => $a['name']
        ));
        set_transient( 'shortcode_post', $shortcode_post, 60*60*4 );

    }
        if ($shortcode_post->have_posts())
            while ($shortcode_post->have_posts()):
                $shortcode_post->the_post();

                // WHICH MODULE
                $moduleType = get_field('module_type');
                if($moduleType == 'basic'){
                    include('module-basic.php');
                } elseif($moduleType == 'cta'){
                    include('module-cta.php');
                } elseif($moduleType == 'grid'){
                    include('module-grid.php');
                } elseif($moduleType == 'carousel'){
                    include('module-carousel.php');
                }
            endwhile;
        else
            $out = "No modules match your shortcode";
        wp_reset_query();


    return html_entity_decode($out);

}
add_shortcode( 'module', 'cpt_shortcode' );


// KILL THE SHORTCODE TRANSIENT WHEN A NEW POST IS SAVED
function shortcode_delete_its_transients() {
        delete_transient( 'shortcode_post' );
}

add_action( 'save_post', 'shortcode_delete_its_transients' );

Here is the gist.

0

1 Answer 1

0

You're using it twice differently, but you're not creating a unique transient name, so the second time the shortcode runs it just uses whatever was saved in the transient the first time the shortcode ran.

Come up with some way to uniquely identify the transient, like:

set_transient( 'shortcode_post_' . sanitize_key( $a['name'] ), $shortcode_post, 60*60*4 );

That said, I don't know that there's really much advantage to saving that result to a transient at all.

3
  • Ahh thank you that make sense. These modules make up the homepage, I needed a way to package content into chunks and display, so in this case the transient should save me quite a bit if the homepage was hit 10k times in a day, am I wrong there? Commented Aug 5, 2014 at 2:17
  • fetching a transient requires a query to the database, so you're just swapping one query for a different query. if it was a complex query, or if you were caching multiple queries into one transient, then it makes more sense.
    – Milo
    Commented Aug 5, 2014 at 2:31
  • huh, ok. Well I thought I had figured a way to speed things up a bit. thanks again Commented Aug 5, 2014 at 2:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.