Last active
October 24, 2021 02:15
-
-
Save jasperf/a3eb0a24e64efafe4c1c00748bee97f5 to your computer and use it in GitHub Desktop.
Randomly load custom post type data- code by Piet Goosen to https://wordpress.stackexchange.com/a/218760/12260
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/*** | |
* Get the random custom post type ids and store them in a transient | |
*/ | |
function get_random_id( $post_type = '' ) | |
{ | |
$q = []; | |
// Make sure we have a post type set, check if it exists and sanitize | |
$post_type = filter_var( $post_type, FILTER_SANITIZE_STRING ); | |
if ( !$post_type ) | |
return $q; | |
if ( !post_type_exists( $post_type ) ) | |
return $q; | |
// The post type exist and is valid, lets continue | |
$transient_name = 'rand_ids_' . md5( $post_type ); | |
// Get the transient, if we have one already | |
if ( false === ( $q = get_transient ( $transient_name ) ) ) { | |
$args = [ | |
'post_type' => $post_type, | |
'posts_per_page' => -1, | |
'fields' => 'ids', // get only post ID's | |
// Add any additional arguments | |
]; | |
$q = get_posts( $args ); | |
// Set the transient | |
set_transient( $transient_name, $q, 30*DAY_IN_SECONDS ); | |
} // endif get_transient | |
return $q; | |
} | |
/*** | |
* The transient is set for 30 days, so we need to flush and recreate the transient as | |
* soon as we publish a new custom post type post. | |
*/ | |
add_action( 'transition_post_status', function ( $new_status, $old_status, $post ) | |
{ | |
// Make sure we only target our specific post type | |
if ( 'advertising' !== $post->post_type ) | |
return; | |
global $wpdb; | |
// Delete the transients | |
$wpdb->query( "DELETE FROM $wpdb->options WHERE `option_name` LIKE ('_transient%_rand_ids_%')" ); | |
$wpdb->query( "DELETE FROM $wpdb->options WHERE `option_name` LIKE ('_transient_timeout%_rand_ids_%')" ); | |
// Lets rebuild the transient | |
get_random_id( $post->post_type ); | |
}, 10, 3 ); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/*** | |
* Advert injector filter `the_content` can look something like the following where one checks if ad is checked as | |
* true to load, it is single and not admin to then get an array of the post IDs here using advertising as the CPT and | |
* then display content using an ACF field | |
*/ | |
add_filter( 'the_content', 'prefix_insert_post_ads' ); | |
function prefix_insert_post_ads( $content ) { | |
// checkbox to show ad, default true | |
if ( get_field('show_advertisement') ) { | |
if ( is_single() && | |
! is_admin() | |
) { | |
// Get the array of post ID's | |
$post_type_posts = get_random_id( 'advertising' ); | |
// Make sure we have posts | |
if ( $post_type_posts ) { | |
// Get the post object of the id first in line | |
shuffle( $post_type_posts ); | |
$random_ad = get_post( $post_type_posts[0] ); | |
// Display the post content | |
$link = addhttp( get_field('advertisement_link', $random_ad->ID)); | |
$image = get_field('upload_advertisement', $random_ad->ID); | |
// get html | |
$ad_code = '<a href="'.$link.'" target="_blank"><img src="'.$image.'" /></a>'; | |
// show ad after # paragraphs | |
$show_after = get_field('advertisement_show_after'); | |
// return appended $content | |
return prefix_insert_after_paragraph( $ad_code, $show_after, $content ); | |
} | |
} | |
} | |
return $content; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/*** | |
* Display the post content using acf field. This can be used in the area where you want | |
* to load the data. Just need to make sure the post ids were loaded | |
*/ | |
$link = addhttp( get_field('advertisement_link', $random_ad->ID)); | |
$image = get_field('upload_advertisement', $random_ad->ID); | |
// get html | |
$ad_code = '<a href="'.$link.'" target="_blank"><img src="'.$image.'" /></a>'; | |
// show ad after # paragraphs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment