• mike morales

    (@mike-morales)


    Hi, im building a select for filter my media library by DAY in the admin, i have this code but the line $wp_query->set(‘post_date’, $_GET[‘fecha_filter’]); not is working this is the code:

    add_filter('parse_query', 'node_admin_posts_filter');
    
    add_action('restrict_manage_posts', 'node_admin_posts_filter_restrict_manage_posts');
    
    function node_admin_posts_filter($wp_query)
    {
    
        if (is_admin() && isset($_GET['fecha_filter']) && $_GET['fecha_filter'] != '') :
    
            $original_query = $wp_query;
    
            $wp_query->set('post_date', $_GET['fecha_filter']);
    
            $wp_query = $original_query;
    
            wp_reset_postdata();
    
        endif;
    
    }
    
    function node_admin_posts_filter_restrict_manage_posts()
    {
    
        global $wpdb;
    
        $get_posts = $wpdb->get_results("SELECT *, DATE_FORMAT($wpdb->posts.post_date,'%Y-%m-%d') AS niceDate FROM $wpdb->posts WHERE post_mime_type='application/pdf' GROUP BY DATE_FORMAT($wpdb->posts.post_date, '%Y-%m-%d') ORDER BY $wpdb->posts.post_date DESC");
    
        echo '<select name="fecha_filter">';
    
        echo '<option value="">Filtrado por día</option>';
    
        $current = isset($_GET['fecha_filter']) ? $_GET['fecha_filter'] : '';
    
        foreach ($get_posts as $get_post) :
    
            $select = null;
    
            if ($current == $get_post->niceDate) {
                $select = ' selected="selected"';
            }
    
            echo '<option value="' . $get_post->niceDate . '" ' . $select . '>' . $get_post->niceDate . '</option>';
    
        endforeach;
    
    }
  • The topic ‘Hooks Problem Filter’ is closed to new replies.