最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Only a part of array is stored in transient – what could be causing this?

matteradmin6PV0评论

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?

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?

Share Improve this question asked Apr 9, 2019 at 7:38 JussiJussi 234 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

You are using get_the_id() template tag without proper loop.

Your code should be something 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 $the_post) {
                $value1 = get_post_meta( $the_post->ID , '_town', true );
                $value2 = get_post_meta( $the_post->ID , '_quarteroftown', true );
                $value3 = get_post_meta( $the_post->ID , '_street_name', true );
                $value4 = get_post_meta( $the_post->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 );
}; ?>

There is an issue with your code. You are querying the posts, and looping through them without setting the global $post variable. In this kind of loops you can't use get_the_ID() function, as it will return the ID of the currently global $post variable which in your case could be anything, depending on the context of your code.

See the difference:


$query = new WP_Query( [ 'post_type' => 'my_custom_post_type', 'posts_per_page' => - 1, ] );

// First case
if ($query->have_posts()) {
    while($query->have_posts()) {
        // This actually sets the global $post variable to the current post in the loop
        $query->the_post();

        // Here you can use context-dependable functions
        $ID = get_the_ID();
        $title = get_the_title();
    }
}

// You should reset the globals to the previous state
wp_reset_query();

// The Second case
// Please don't use $post variable in your loops, it can sometimes interfere
// with the global $post variable
$my_posts = $query->get_posts();
foreach ($my_posts as $my_post) {
    // here $post is not the global variable, so we can't use the same functions
    $ID = $my_post->ID;
    $title = $my_post->post_title;
}

You can read more about the WP_Query on the WP official documentation.

Post a comment

comment list (0)

  1. No comments so far