• Resolved matdeweb

    (@matdeweb)


    Hi,

    I would like to add a filter by post’ year but with a list of year, is it possible?

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support fesupportteam

    (@fesupportteam)

    Hi @matdeweb

    Perhaps the best solution here would be creating a custom field that pulls only the post-year data and saves it as a value. This way, you can create a filter based on it in the form of a simple list of years.

    Best Regards,
    Victor

    Thread Starter matdeweb

    (@matdeweb)

    ok, good idea. I know hot to create custom field but how update automaticaly the post-year?

    regards,

    Plugin Support fesupportteam

    (@fesupportteam)

    HI @matdeweb

    That would require a custom code which will be fired on the post save/update.
    Something like this (please note that this is not a tested code/solution this is just an example):

    <?php
    add_action('save_post', 'update_post_year_custom_field', 10, 3);

    function update_post_year_custom_field($post_id, $post, $update) {
    // Prevent autosave from triggering the update
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
    return;
    }

    // Check if this is a revision or an unsupported post type
    if (wp_is_post_revision($post_id) || !in_array($post->post_type, ['post'])) {
    return;
    }

    // Verify user permissions
    if (!current_user_can('edit_post', $post_id)) {
    return;
    }

    // Get the post's publish date
    $post_date = get_the_date('Y-m-d', $post_id);
    if ($post_date) {
    // Extract the year
    $year = date('Y', strtotime($post_date));
    // Update the custom field 'post_year' with the year
    update_post_meta($post_id, 'post_year', $year);
    }
    }
    ?>

    Best regards
    Victor

    Thread Starter matdeweb

    (@matdeweb)

    Oh thank you! It works very fine!!!

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘filter by year’ is closed to new replies.