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


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/

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



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

, , , , Sioban 4 years 2020-03-29T12:51:27-05:00 0 Answers 111 views 0

Leave an answer