Skip to content

Instantly share code, notes, and snippets.

@RadGH
Created June 1, 2020 00:06
Show Gist options
  • Save RadGH/452492a502ec09938cdd29cfdef70022 to your computer and use it in GitHub Desktop.
Save RadGH/452492a502ec09938cdd29cfdef70022 to your computer and use it in GitHub Desktop.
WP Rocket won't cache pages with these related posts
<?php
// get list of category IDs that this post belongs to
$cats = array();
if ( $categories = get_the_category() ) {
foreach ( $categories as $category ) {
$cats[] = $category->term_id;
}
}
$blogname = get_bloginfo( 'name', 'display' );
$args_array = array(
'related' => array(
'title' => 'Related on ' . $blogname,
'args' => array(
'posts_per_page' => 6,
'post__not_in' => array( get_the_id() ),
'category__in' => $cats,
'meta_key' => 'cn_engagement',
'orderby' => 'meta_value_num',
),
),
'popular' => array(
'title' => 'Popular on ' . $blogname,
'args' => array(
'posts_per_page' => 6,
'post__not_in' => array( get_the_id() ),
'meta_key' => 'cn_featured',
'orderby' => array(
'meta_value_num' => 'DESC',
'date' => 'DESC',
),
),
),
'trending' => array(
'title' => 'Trending on ' . $blogname,
'args' => array(
'posts_per_page' => 6,
'post__not_in' => array( get_the_id() ),
'meta_query' => array(
'relation' => 'OR',
'sort_trending' => array(
'key' => 'cn_trending',
'value' => 1,
),
array(
'key' => 'cn_trending',
'value' => 0,
),
array(
'key' => 'cn_trending',
'compare' => 'NOT EXISTS',
),
),
'meta_key' => 'cn_engagement',
'orderby' => array(
'sort_trending' => 'DESC',
'meta_value_num' => 'DESC',
'date' => 'DESC',
),
),
),
'also-on' => array(
'title' => 'Also on ' . $blogname,
'args' => array(
'posts_per_page' => 24,
'post__not_in' => array( get_the_id() ),
'orderby' => 'rand',
),
),
);
// temporarily disabled
return;
foreach ( $args_array as $key => $value ) :
$recommended_posts = new WP_Query( $value['args'] );
if ( $recommended_posts->have_posts() ):
?>
<section class="post-recommended post-recommended-<?php echo $key; ?>>">
<h2 class="underlined-title"><?php echo $value['title']; ?></h2>
<div class="article-grid">
<?php
while( $recommended_posts->have_posts() ):
$recommended_posts->the_post();
?>
<article>
<?php cn_post_thumbnail( 'thumbnail', true ); ?>
<a href="<?php the_permalink(); ?>" rel="bookmark">
<h3 class="entry-title"><?php the_title(); ?></h3>
</a>
</article>
<?php
endwhile;
wp_reset_postdata();
?>
</div>
</section>
<?php
// Maybe display an ad below this group
cn_display_below_post_ad( $key );
endif;
endforeach;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment