• Is it possible to have it display users that have a meta field that does not equal today’s date?

    I need to display a list of users that have not submitted a form. I’ve created a custom field for the users that will get filled with today’s date upon submission. I’m trying to find an elegant way to display the users that don’t have today’s date in this meta field.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author HelgaTheViking

    (@helgatheviking)

    Hi Ed,

    Simple User Listing supports meta fields in the shortcode and you can use meta_compare to change the comparison operator. However, today’s date would be dynamic so I don’t think you could do this via the shortcode parameters.

    What you could do is something like what’s in the FAQ and create a query ID and then filter the meta_query array to test for what you need.

    good luck!

    Thread Starter Ed Ellingham

    (@spednix)

    Hi Helga,

    Amazing that you responded so quickly!

    I apologize, as PHP isn’t my jam.

    Would you mind critiquing this? I feel like I might be close, but I’m always showing the user in my list, so it’s obviously not working ??

    
    //Today Query for Simple User Listings
    add_filter( 'sul_user_query_args', 'sul_custom_meta_query', 10, 2 );
    
    function sul_custom_meta_query( $args, $query_id ){
    	$todaysdate = date('F j, Y');
        // Checking the query ID allows us to only target a specific shortcode.
        if( $query_id == 'time_meta_query' ){
                $args['meta_query'] = array(
    				'key'	=>	'last_check-in',
    				'value'	=>	$todaysdate,
    				'compare'	=>	'!=',
    				'type'	=>	'CHAR',
    				);
    		}
        return $args;
    }
    Plugin Author HelgaTheViking

    (@helgatheviking)

    Hi Ed,

    Sorry I missed your reply. It looks ok to me. What if you try running a test WP_Query with the same args as your user list? I don’t work on this plugin very often, but it does have some caching/transients that might be stashing an incorrect result. So I would test the query outside the scope of my plugin first.

    
    $args = array (
    	'meta_query' => array(
    		'key'	=>	'last_check-in',
    		'value'	=>	$todaysdate,
    		'compare'	=>	'!=',
    		'type'	=>	'CHAR',
    		)
    );
    $my_query = new WP_Query( $args );
    if ( $my_query->have_posts() ) { 
    	while ( $my_query->have_posts() ) { 
    		$my_query->the_post();
    		the_title( '<h2>', '</h2>' );
    	}
    }
    wp_reset_postdata();

    When you get it working outside of SUL, then you can try updating saving any post to trigger the clearing of the transients.

    Thread Starter Ed Ellingham

    (@spednix)

    Hi Helga,

    Thanks! One of my devs figured out the query.

    I do have another question, because the templates are throwing me. I see in other replies to other questions you respond as if all of the code is in content-author.php for removing things (ie, avatar), but it seems like all of the code listing code is in simple-user-listing-template-functions.php and I’m unclear how to translate this into content-author.php.

    Did you change things at one point in how the content-author.php template works?

    Plugin Author HelgaTheViking

    (@helgatheviking)

    Hi Ed,

    At some point, I changed the content-author.php template to be largely action hooks with the different functions attached to the different hooks. I like hooks/functions myself. You can unhook the existing template functions and then add your own.

    As an example, adding this to your child theme’s functions.php would remove the avatar from the output and replace it with the “$name likes Tacos”.

    
    function kia_sul_remove_avatar() {
    	remove_action( 'sul_before_user_loop_author_title', 'sul_template_loop_author_avatar' );
    	add_action( 'sul_before_user_loop_author_title', 'kia_likes_tacos' );
    }
    add_action( 'simple_user_listing_before_loop', 'kia_sul_remove_avatar' );
    
    function kia_likes_tacos() {
          global $user;
          echo $user->display_name . " likes Tacos??";
    }
    

    Or you can override the entire content-author.php template and edit directly there. You lose the hooks and whatever I might add to them, but realistically… that isn’t much of a loss!

    
    if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
    
    global $user;
    
    ?>
    <div id="user-<?php echo $user->ID; ?>" class="author-block">
    
    <?php echo $user->display_name . ' likes Tacos??'; ?>
    
    </div>
    

    I hope that helps… probably should add something to the docu, but a little overwhelmed at the moment. If there’s anything you’ve learned that you think would be helpful, I’d be happy to add anything you write.

    Cheers!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Show users if field doesn’t equal today’s date’ is closed to new replies.