最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

loop - Create a new query in function.php to filter blog posts

programmeradmin1浏览0评论

My goal is to filter blog posts (those which are requested in the blog page - Reading settings) depending on an expiration date, and then to change their category depending on a choosen future category (no more blog post, but medias post for example).

I manage to do this, but only if I include the change_category function in the index.php template (index.php beeing the default template for blog posts).

This creates a problem :

The category of an expired blog post only changes if the blog page is refreshed before the medias page loading. In other words, an expired blog post could be displayed on the medias page - and no more on the blog page- if and only if the blog page has been refreshed. And it's logic.

From the beginning, I would like to add the change_category function in the function.php file, but I can't manage to create a valid query. I always have this error :

PHP Fatal error:  Allowed memory size of 4294967296 bytes exhausted (tried to allocate 20480 bytes) in /home/mywebsite.fr/wp-includes/class-wp-query.php.

I tried to use the variable global $post, get_posts(), ajax and many other things, but I'm hitting a wall. Here is my code in functions.php

add_filter('pre_get_posts', 'si0b_change_category');

function si0b_change_category()
{
    $args = [
        'posts_per_page' => 10,
        'page'           => 10,
        'post_type'      => 'post',
    ];

    $post_list = get_posts($args);

    if ($post_list) {
        foreach ($post_list as $blog_post) {

            $post_future_category = get_post_meta($blog_post->ID, '_si0b_future_category_value', true);

            $post_expiration_date = get_post_meta($blog_post->ID, '_si0b_expiration_date_value', true);

        }

        wp_reset_postdata();
    }
}

Thank you if you've got time to try to help me !

发布评论

评论列表(0)

  1. 暂无评论