只有数组的一部分存储在瞬变模式中--这可能是什么原因?

时间:2019-04-09 作者:Jussi

我已经创建了一个autosuggest数组,希望将其存储在瞬态中。我的代码如下所示:

<?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 );
}; ?>
如果我没有设置一个瞬态,而只是使用$suggest对象,那么所有值都正确地存在于数组中。当我添加如上所示的瞬态功能时,瞬态中只有$value1和$value2字符串,没有\\u streetname或\\u supplier\\u postcode。

给我的wtf火上浇油:如果我每次都简单地命令删除瞬态,它就会开始工作!请参见下面的代码:

<?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\');
我不知所措。到底是什么导致了这种行为?

2 个回复
最合适的回答,由SO网友:Qaisar Feroz 整理而成

你正在使用get_the_id() template tag 没有适当的loop.

您的代码应该是这样的

<?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 );
}; ?>

SO网友:djboris

您的代码有问题。您正在查询帖子,并在其中循环,而不设置全局$post 变量在这种循环中,您不能使用get_the_ID() 函数,因为它将返回当前全局$post 变量,在您的情况下可以是任何内容,具体取决于代码的上下文。

查看差异:


$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;
}

您可以阅读更多关于WP_Query 在WP上official documentation.

相关推荐

get_category_parents to array

好的,我可以从wp下拉列表中选择。此选项是一个类别。我使用此类别查找它的家长。这似乎输出了一个字符串。此字符串应转换为数组。然后我需要遍历数组,并将类别名称更改为类别ID。但出于某种原因,这似乎不起作用。我可能做错了什么?我把不起作用的地方注释掉了。(此数组用于post\\u类别以创建新的帖子。)//Create array $categoriesArray = []; //Get choice from wp-dropdown $selected_val = $_POS