$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>filters - Wordpress: Issue with filtering users using date range|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

filters - Wordpress: Issue with filtering users using date range

matteradmin9PV0评论

I'm trying to add a date range filter for wordpress admin users table to filter users that have posted ads between the selected range. I've managed to add the date range inputs.

I have 2 issues with this approach.

  1. When I hit filter the selected date doesn't bind to the query (like date_from=2018-11-28), with the text input.

    <input type="text" id="date-from" name="date_from" autocomplete="off">

    Then I used a hidden select input with the same name and set the value using jquery when date input is changed.( This is not good, but solve the issue)

    What's the issue with the input. Can't I use a text input in filters ?

  2. To add the filter query I used pre_get_users . But it doesn't filter users

    add_filter( 'pre_get_users', 'filter_users' );
    function filter_users( $query ) {
    global $pagenow;
    
    if ( is_admin() &&  'users.php' == $pagenow && isset( $_GET[ 'date_from' ] ) || isset( $_GET[ 'date_to' ] ) ) {
    
        $date_from = isset( $_GET[ 'date_from' ] ) ? $_GET['date_from'] : null;
        $date_to = isset( $_GET[ 'date_to' ] ) ? $_GET['date_to'] : null;
    
        $date_where = '';
    
        if( ! empty($date_from) > 0 && ! empty($date_to) > 0){
            $date_where = "DATE(wp_posts.post_date) >= '$date_from' AND DATE(wp_posts.post_date) < '$date_to'";
        }else if( ! empty($date_from)){
            $date_where = "DATE(wp_posts.post_date) >= '$date_from'";
        }else if( ! empty($date_to)){
            $date_where = "DATE(wp_posts.post_date) < '$date_to'";
        }
    
        if( empty($query->query_where) ){
            $query->query_where = "INNER JOIN wp_posts ON wp_posts.post_author = wp_users.ID WHERE $date_where";
        }else{
    
            $query->query_where = str_replace( "WHERE 1=1', 'INNER JOIN wp_posts ON wp_posts.post_author = wp_users.ID WHERE 1=1 AND $date_where", $query->query_where);
        }
    
    }
    }
    

What is the reason for this issue.

Thanks in advance

Post a comment

comment list (0)

  1. No comments so far