I have created an autosuggest array that I want to store in a transient. My code looks like this:
<?php
// Get any existing copy of our transient data
$suggest = get_transient('suggest');
if ( false === ( $suggest = get_transient( 'suggest' ) ) ) {
// It wasn't there, so regenerate the data and save the transient
$suggest = array();
// We will loop a custom post type and fetch custom meta from each post to be added into the autosuggest array
$query = new WP_Query( array( 'post_type' => 'my_custom_post_type', 'posts_per_page' => -1, ) );
$posts = $query->posts;
foreach($posts as $post) {
$value1 = get_post_meta( get_the_ID() , '_town', true );
$value2 = get_post_meta( get_the_ID() , '_quarteroftown', true );
$value3 = get_post_meta( get_the_ID() , '_street_name', true );
$value4 = get_post_meta( get_the_ID() , '_supplier_postcode', true );
if (!in_array($value1, $suggest))
{
$suggest[] = $value1;
}
if (!in_array($value2, $suggest))
{
$suggest[] = $value2;
}
if (!in_array($value3, $suggest))
{
$suggest[] = $value3;
}
if (!in_array($value4, $suggest))
{
$suggest[] = $value4;
}
}
set_transient( 'suggest', $suggest, HOUR_IN_SECONDS );
}; ?>
If I do not set a transient and simply use the $suggest object, all values are properly there in the array. When I add the transient functionality as seen above, the transient will only have $value1 and $value2 strings in it, none of _streetname or _supplier_postcode.
Further fuel to my wtf-fire: if I simply order the transient to be deleted every time, it starts working! See code below:
<?php
// Get any existing copy of our transient data
delete_transient('suggest'); // ADDING THIS AND FORCING REGEN OF TRANSIENT EVERY TIME FIXES THE PROBLEM!
$suggest = get_transient('suggest');
I am at a loss. What on earth can be causing this behaviour?