是否使用临时API缓存所有类别中所有帖子的查询?

时间:2017-06-24 作者:lowtechsun

我用它来显示所有类别中的所有帖子。

$args_cat = array(
    // order by category name ascending
    \'orderby\' => \'name\',
    \'order\' => \'ASC\',
    // get only top level categories
    \'parent\' => 0
);
$categories = get_categories($args_cat);

// Full posts query
// if there are categories filled with posts
if (!empty ($categories) && !is_wp_error( $categories )) {

    foreach ($categories as $category) {

        // Query all posts by slug inside each category
        $args_category_posts = array(
            \'post_type\' => \'post\',
            // The category slug and category name we get from the foreach over all categories
            \'category_name\' => $category->slug
        );

        $query = new WP_Query($args_category_posts);
        if ($query->have_posts()) {
            while ($query->have_posts()) {
                $query->the_post(); ?>
                <article class="<?php echo $category->slug ?>-article">
                    <h2 class="<?php echo $category->slug ?>-article-title">
                        <a href="<?php echo get_permalink() ?>"><?php echo get_the_title() ?></a>
                    </h2>
                    <p class="<?php echo $category->slug ?>-post-info">
                        <?php the_time(\'d. m. Y\') ?>
                    </p>
                    <div <?php post_class() ?> >
                        <?php the_content(); ?>
                    </div>
                </article> <?php
            }
        } // end loop
    } // end foreach
wp_reset_postdata() ;
} // end if there are categories filled with posts
已经阅读this, this, thisthis 我想知道我是否在做下面的事情。

// Transient API all categories and all posts
$query_categories = get_transient(\'cached_categories\');
if ( false === $query_categories){
    $args_cat = array(
        // order by category name ascending
        \'orderby\' => \'name\',
        \'order\' => \'ASC\',
        // get only top level categories
        \'parent\' => 0
    );
    // Instead of caching a WP Query I cache \'get_categories($args_cat)\', OK to use it like this?
    $query_categories = get_categories($args_cat);
    set_transient(\'cached_categories\', $query_categories, DAY_IN_SECONDS );
}

// Full posts query
// if there are categories filled with posts
if (!empty ($query_categories) && !is_wp_error( $query_categories )) {

    foreach ($query_categories as $category) {

        $query_category_posts = get_transient(\'cached_posts\');
        if ( false === $query_category_posts ){

            // Query all posts by slug inside each category
            $args_category_posts = array(
                \'post_type\' => \'post\',
                // The category slug and category name we get from the foreach over all categories
                \'category_name\' => $category->slug
            );

            // Here I cache the WP_Query, though this runs for all categories.
            // So am I storing multiple transients here?
            $query_category_posts = new WP_Query($args_category_posts);
            set_transient( \'cached_posts\', $query_category_posts, DAY_IN_SECONDS );
        }

        if ($query_category_posts->have_posts()) {
            while ($query_category_posts->have_posts()) {
                $query_category_posts->the_post(); ?>
                <article class="<?php echo $category->slug ?>-article">
                    <h2 class="<?php echo $category->slug ?>-article-title">
                        <a href="<?php echo get_permalink() ?>"><?php echo get_the_title() ?></a>
                    </h2>
                    <p class="<?php echo $category->slug ?>-post-info">
                        <?php the_time(\'d. m. Y\') ?>
                    </p>
                    <div <?php post_class() ?> >
                        <?php the_content(); ?>
                    </div>
                </article> <?php
            }
        } // end loop
    } // end foreach
wp_reset_postdata() ;
} // end if there are categories filled with posts
输出正常,所有类别和帖子都显示良好,但类别和帖子现在是否已正确缓存?

在代码中,我对设置瞬态的两个地方进行了注释,但不确定这是否可以用于get_categories() 或在foreach 适用于该类别中的所有职位。

Edit
瞬态适用于get_categories(). 这个var_dump 输出显示了保存可用类别的WP\\u Term对象数组。

$query_categories = get_categories($args_cat);
var_dump($query_categories);
set_transient(\'cached_categories\', $query_categories, DAY_IN_SECONDS );
Edit
内部瞬态foreach 仅获取第一个类别及其帖子,并重复该类别的数量。除第一个类别以外的其他类别不存在,这可能是因为在第一轮foreach 它已满,因此循环仅显示第一类的帖子。

Now how can I make a new transient that holds the posts per category on every cycle of the foreach for the number of categories?

对瞬态API完全陌生,并试图充分利用它。尝试调整代码并保持最佳实践,如果这一切都是错误的使用潜伏或没有使用,因为它不工作,请让我知道。如果能给我一些指导或者我已经掌握了使用瞬态的“方法”,我将不胜感激。

Solution
this answer 我必须为每个类别创建的瞬态提供类别名称(slug)。否则,只有第一个类别存储在瞬态中,一旦第二个循环中的类别已满,则不会更改。因此,只显示第一类的员额。

仅使用$category 因为瞬态变量自$category 是一个对象,需要一个字符串,所以我将其更改为$category->slug 现在它开始工作了。

这是在两个地方完成的,获取和设置瞬态。非常感谢。

// Transients API all categories and all posts
$query_categories = get_transient(\'cached_categories\');
if ( false === $query_categories){
    $args_cat = array(
        // order by category name ascending
        \'orderby\' => \'name\',
        \'order\' => \'ASC\',
        // get only top level categories
        \'parent\' => 0
    );
    // Instead of caching a WP Query I cache \'get_categories()\'.
    $query_categories = get_categories($args_cat);
    // var_dump($query_categories);
    set_transient(\'cached_categories\', $query_categories, DAY_IN_SECONDS );
}

// Full posts query
// if there are categories filled with posts
if (!empty ($query_categories) && !is_wp_error( $query_categories )) {

    foreach ($query_categories as $category) {

        // var_dump($category);
        $query_category_posts = get_transient(\'cached_posts_\' . $category->slug );
        if ( false === $query_category_posts ){

            // Query all posts by slug inside each category
            $args_category_posts = array(
                \'post_type\' => \'post\',
                // The category slug and category name we get from the foreach over all categories
                \'category_name\' => $category->slug
            );

            // Here I cache the WP_Query, though this runs for all categories.
            // Because of that the \'$category->slug\' is used to serve a string and not an object.
            $query_category_posts = new WP_Query($args_category_posts);         
            set_transient( \'cached_posts_\' . $category->slug , $query_category_posts, DAY_IN_SECONDS );
        }

        if ($query_category_posts->have_posts()) {
            while ($query_category_posts->have_posts()) {
                $query_category_posts->the_post(); ?>
                <article class="<?php echo $category->slug ?>-article">
                    <h2 class="<?php echo $category->slug ?>-article-title">
                        <a href="<?php echo get_permalink() ?>"><?php echo get_the_title() ?></a>
                    </h2>
                    <p class="<?php echo $category->slug ?>-post-info">
                        <?php the_time(\'d. m. Y\') ?>
                    </p>
                    <div <?php post_class() ?> >
                        <?php the_content(); ?>
                    </div>
                </article> <?php
            }
        } // end loop
    } // end foreach
wp_reset_postdata() ;
} // end if there are categories filled with posts

1 个回复
最合适的回答,由SO网友:Nicolai Grossherr 整理而成

您正在将每个类别的每个查询对象保存到相同的瞬态中。因为这种情况发生得很快,而且时间范围是一天,所以您总是会得到第一个类别的查询对象。将临时名称与类别一起变为变量,例如:

$query_category_posts = get_transient(\'cached_posts_\' . $category );
当然,接下来需要使用变量名获取瞬态。

结束

相关推荐

Wp-login.php-将登录用户重定向到自定义URL

当用户访问时/wp-login.php 并且已经登录,如何将其重定向到自定义URL?我发现同样的问题被问到和回答了,只是关于谁重定向到管理面板,但我不想这样做,我想重定向到一个特定的URL。(wp-login.php -- redirect logged in users to custom URL).我特别想将他们重定向到my_home_URL.com/my-account 我将如何实现这一点?如有任何建议或指导,将不胜感激!编辑:我几乎没有成功,但在访问时使用了以下功能wp-login.php 它确实