• Resolved jockebq

    (@jockebq)


    Hi,

    This is a very advanced question, but I am trying to make a function which changes the CSS ID if true. And what I want it to do is to count the current events (the posts).
    This is the function I have come up with:

      if (count($vsel_current_query) > 20) {
          $div_id = 'test2';
      }
      else {
          $div_id = 'test';
      }

    The reason I try with $vsel_current_query is because of this one:
    $output .= get_next_posts_link( __( 'Next »', 'events' ), $vsel_current_query->max_num_pages );
    Which I think is counting the current event posts, and if they are more than the Max Blog posts setting in WordPress it will display the Next button.

    Can you help me figure this one out?

    Thank you!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Guido

    (@guido07111975)

    Hi,

    Cannot promise you that I have a proper solution, but which ID do you want to change? The ID of certain events? The ID of an element of your page? Another ID?

    Guido

    Thread Starter jockebq

    (@jockebq)

    Don’t remember the name right now but I dont think the ID is what matters, but I cannot figure out how to get the number of posts/events on the current day. Do you know which php function to call?

    Plugin Author Guido

    (@guido07111975)

    Hi,

    There’s no function inside my plugin for that, you should write your own function, have found this example.

    Guido

    Thread Starter jockebq

    (@jockebq)

    Aha! Great find, i read through it, but VSEL works differently right? That function applies to the last 24 hours. And your current events will be current date, not last 24 hours, How would you do this for the current day instead?

    Plugin Author Guido

    (@guido07111975)

    Hi,

    Ok, you can retrieve the number of current (today’s) posts by using the current events shortcode query. I’ve created this function:

    
    function vsel_current_events_count() {
    	$today = strtotime( 'today' );
    	$vsel_meta_query = array( 
    		'relation' => 'OR',
    		array( 
    			'key' => 'event-date', 
    			'value' => $today, 
    			'compare' => '==', 
    			'type' => 'NUMERIC'
    		), 
    		array( 
    			'relation' => 'AND', 
    			array( 
    				'key' => 'event-start-date', 
    				'value' => $today, 
    				'compare' => '<=', 
    				'type' => 'NUMERIC'
    			), 
    			array( 
    				'key' => 'event-date', 
    				'value' => $today, 
    				'compare' => '>',
    				'type' => 'NUMERIC'
    			) 
    		) 
    	); 
    	$vsel_query_args = array( 
    		'post_type' => 'event', 
    		'post_status' => 'publish', 
    		'meta_key' => 'event-start-date', 
    		'orderby' => 'meta_value_num', 
    		'meta_query' => $vsel_meta_query
    	); 
    	$vsel_current_query = new WP_Query( $vsel_query_args );
    
    	return $vsel_current_query->post_count;
    }
    

    Now you can use this number elsewhere by calling the function:

    
    vsel_current_events_count()
    

    Guido

    Thread Starter jockebq

    (@jockebq)

    Wow!! That was very nice of you! It works perfectly! Thank you!

    Plugin Author Guido

    (@guido07111975)

    Great 🙂

    If you somehow get a conflict with the original current events query, please change the query name vsel_current_query in this function into something else such as vsel_custom_query.

    Will mark this thread resolved.

    Guido

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

The topic ‘PHP Count current events?’ is closed to new replies.