• Resolved Autosoft B.V.

    (@autosoftbv)


    In our template we display random posts / posttypes in different areas of the website.
    We made a custom query and use ‘orderby’ => ‘rand’ for the elements to display randomly.

    However, when the W3 Total Cache plugin is enabled, it only randomizes the posts on first load.
    When purging al caches, it rebuilds to a new one, after that, the same post will be repeated untill the cache is purged again.

    Since we use this functionality in the footer of our page (displaying employees and quotes) we are not able to “exclude” just one page.

    How can we get ‘orderby’ => ‘rand’ to work in combination with the W3 Total Cache plugin?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi @autosoftbv,

    Thank you for your question and I am happy to help in any way I can.

    You can wrap your custom loop with ‘mfunc’ in your footer template to tell W3TC not to cache that section of code, but there is some setup required.

    The first thing you will need to do is add the W3TC_DYNAMIC_SECURITY constant to your wp-config.php.

    define('W3TC_DYNAMIC_SECURITY', 'useanyvalueyoulike');

    Once that constant is created you can wrap the custom loop in your footer with mfunc and the security constant.

    <!--mfunc <?php echo W3TC_DYNAMIC_SECURITY; ?> -->
    
    your custom post loop here
    
    <!--mfunc <?php echo W3TC_DYNAMIC_SECURITY; ?> -->

    I hope that helps you keep your employee quotes randomized and please let me know if you have any other questions.

    Thread Starter Autosoft B.V.

    (@autosoftbv)

    Thank you for your reply @joe9663 ,

    Unfortunately, I can not get this to work.

    I’ve tried:

    – setting Page cache to Disk:basic
    – activated Late initialization on the page cache.
    – added “mfunc” to the ignored comment stems on the Minify page.
    – added the folowing code to wp-config.php:
    define( 'W3TC_DYNAMIC_SECURITY', md5( rand( 0, 999999 ) ) );

    After that, I added the folowing code to the template to see if it works

    <!--mfunc <?php echo W3TC_DYNAMIC_SECURITY; ?> -->
    <?php echo 'The time is '.date( 'H:i:s', time() ); ?>
    <!--/mfunc <?php echo W3TC_DYNAMIC_SECURITY; ?> -->

    Unfortunately, the code is still being cached.

    The plugin is currently enabled in community mode.
    Could it be that this functionality only works in the Pro version?
    or am i just missing something?

    Hi @autosoftbv,

    The value set for W3TC_DYNAMIC_SECURITY cannot be random and needs to be a static value due to variable matching requirements. I found this Gist from one of the plugin developments stating that as a requirement.

    Unfortunately the fragment caching ability is only available in the Pro version and will not function correctly if you are using the community version.

    Let me know if you have any other questions, I am happy to help!

    Thread Starter Autosoft B.V.

    (@autosoftbv)

    We’ve also tried it with a static value, to no avail.

    so I guess this must be a Community / Pro version issue.

    Thank you for your help so far.
    Appreciate it.

    Topic closed.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Orderby => Rand’ is closed to new replies.