Skip to content

Instantly share code, notes, and snippets.

@DouglasdeMoura
Last active October 23, 2017 18:48
Show Gist options
  • Save DouglasdeMoura/7e1fb7e30fab011654c8cfc5db1820c2 to your computer and use it in GitHub Desktop.
Save DouglasdeMoura/7e1fb7e30fab011654c8cfc5db1820c2 to your computer and use it in GitHub Desktop.
Executar WP_Query randomicamente a cada 30 minutos
<?php
$cache_key = 'categoria-da-home';
if ( ! $ids = get_transient( $cache_key ) ) {
//Essa query pega apenas os IDs dos posts
$query = new WP_Query( [
'fields' => 'ids',
'posts_per_page' => 15,
'cat' => 5, //ID da categoria
'orderby' => 'rand',
] );
$ids = $query->posts;
set_transient( $cache_key, $ids, 1800 ); //Aqui eu salvo os IDs dos posts no cache por 30 minutos (1800 segundos)
}
//Aqui eu pego apenas os posts que eu salvei no cache. Isso mudará a cada 30 minutos
$query = new WP_Query( [
'post__in' => $ids,
] );
// while ( $query->have_posts() ) ..
if ( $query->have_posts() ) {
echo '<ul>';
while ( $query->have_posts() ) {
$query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
wp_reset_postdata();
} else {
echo '<p>Nenhum post encontrado nesta categoria :(</p>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment