Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WordPress Transients API: Show content to logged-in users, or otherwise access PHP superglobals
<?php
if( current_user_can( 'read' ) ) {
if( false === ( $output = get_transient( 'my-insider-news' ) ) ) {
$blurbs = new WP_Query(
array(
'post_status' => 'publish',
'posts_per_page' => 5,
'orderby' => 'date',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-aside' ),
'operator' => 'IN'
),
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( 'insiders' ),
'operator' => 'IN'
)
)
)
);
$output = '<ul>';
if( $blurbs->have_posts() ) {
while( $blurbs->have_posts() ) {
$blurbs->the_post();
$output .= '<li><a href="' . the_permalink() . '"><h3>' . the_title() . '</h3></a>';
$output .= the_excerpt() . '</li>';
}
} else {
$output .= '<li>No insider information today.</li>';
}
$output .= '</ul>';
//transient will last a half-hour
set_transient('my-insider-news', $output, 30 * MINUTE_IN_SECONDS);
}
} else {
$output = "<p>Join the Insiders to get news before everyone else!</p>";
}
echo $output;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.