WordPress.org

Ready to get started?Download WordPress

Forums

Events Manager
[resolved] Filter custom fields using EM_Events:get (20 posts)

  1. thegreyspot
    Member
    Posted 3 months ago #

    HI,

    Love the plugin!

    I am using Advance Custom Fields plugin to add a few extra options to the Events posts. Idealy, I'd like to perform a get like this:

    $featured_events= EM_Events::get( array( 'limit' => 3, 'array'=> true, 'event_attributes' => Array ( 'publish_to_university_homepage' => 1 , 'university_homepage_featured_event' => 1 ) ) );

    But that completely ignores my Event_attributes stuff. You can see my custom fields show up in the event object like so:

    ...
    [event_date_modified] => 2014-01-12 17:13:10 [event_attributes] => a:1:{s:30:"publish_to_university_homepage";s:1:"0";} [blog_id] => 1 [group_id] => 0 [recurrence] => 0 [recurrence_interval] => [recurrence_freq]

    Do you think this is possible?
    Thanks!
    http://wordpress.org/plugins/events-manager/

  2. caimin_nwl
    NetWebLogic Support
    Posted 3 months ago #

    Hi,

    Glad you're enjoying the plugin.

    What do you mean when you say, "But that completely ignores my Event_attributes stuff". You mean it ignores custom attributes / fields you've added using Events Manager?

  3. thegreyspot
    Member
    Posted 3 months ago #

    It just lists all of events and ignores the "publish_to_university_homepage =>1" attribute i have in my get() request.

    Keep in mind, I added this custom field using Advanced Custom Fields (ACF), and NOT the events Manager custom fields. I did this because it made it easier with the other fields I have for posts. Do I need to use the Events Manager custom fields? If so, how would I form the get requests in this case? And can I apply the same custom fields across my whole multisite network?

    Thanks

  4. angelo
    NetWebLogic Support
    Posted 3 months ago #

    maybe you can try additional coding like

    if ( in_array('news',$featured_events->event_attributes) ){ 
    
    }else{
    
    }
  5. thegreyspot
    Member
    Posted 3 months ago #

    You mean grab a couple of events and then filter through them with this kind of if statement?

    That would be sort inconvenient, because I wont know how many posts to grab before finding what i need.

    Thanks

  6. caimin_nwl
    NetWebLogic Support
    Posted 3 months ago #

    I think Angelo means you'd just use that code in an if statement to discard, or keep, the events you wanted.

  7. thegreyspot
    Member
    Posted 3 months ago #

    Right thats what I understood too? But I'm saying that wouldn't be very efficient. Do you have any recommendations on how I should do my search? Should I just go day by day until i have the number of events I want?

  8. thegreyspot
    Member
    Posted 3 months ago #

    I am trying to do what Angelo_nwl suggested. However, I don't know how to parse

    [event_attributes] => a:1:{s:30:"publish_to_university_homepage";s:1:"0";}

    Which is what is in my EM_Events array. PHP thinks its a string, so I can't use in_array...

    Any ideas?

  9. angelo
    NetWebLogic Support
    Posted 3 months ago #

    you can use php serialize() - http://www.php.net/manual/en/function.serialize.php

    eg.

    $event_attributes = serialize ($featured_events->event_attributes);
    if ( in_array('news',$event_attributes) ){ 
    
    }else{
    
    }
  10. thegreyspot
    Member
    Posted 3 months ago #

    @angelo_nwl I did that but in_array complains that its still a string. So I'm guessing it was able to serialize?

    I mean, this doesnt look very normal to me:
    [event_attributes] => a:1:{s:30:"publish_to_university_homepage";s:1:"0";}

  11. caimin_nwl
    NetWebLogic Support
    Posted 3 months ago #

    Where are you using that code? I think it would be helpful to see the chunk of code you're using.

  12. thegreyspot
    Member
    Posted 3 months ago #

    function getEventsWithFilter($limit, $conditions, $blog = ""){
    	$grab = 5;
    	$offset = 0;
    	$events = [];
        while(count($events) < $limit){
        	$tempEvents = EM_Events::get( array('limit' => $grab,
        										'offset'=> $offset,
        										'blog' => $blog,
        										'array'=> true));
        	if(count($tempEvents) == 0)
        		break;//no more events to save
        	//print_r($tempEvents);
        	foreach($tempEvents as $tempEvent){
        		$EA = serialize($tempEvent['event_attributes']);
        		print_r($EA);
        		if($EA){
        			foreach($conditions as $condition){
        				//echo $condition;
        				if(in_array("publish_to_university_homepage", $EA))
            				array_push($events, $tempEvent);
            		}
            	}
            }
            $offset += $grab;
        }
    
        //Trim off extra arrays, since we do 2 at a time
        return array_slice($events, 0, $limit);
    }

    This gives me this error on the in_array line:

    Warning: in_array() expects parameter 2 to be array, string given in

    Also the print_r, clearly looks like an string.

    Does that help? Thanks

  13. angelo
    NetWebLogic Support
    Posted 3 months ago #

    you need

    $tempEvents->event_attributes

    instead of

    $tempEvent['event_attributes']

  14. thegreyspot
    Member
    Posted 3 months ago #

    Yes I have tested that too.

    function getEventsWithFilter($limit, $conditions, $blog = ""){
    	$grab = 5;
    	$offset = 0;
    	$events = [];
        while(count($events) < $limit){
        	$tempEvents = EM_Events::get( array('limit' => $grab,
        										'offset'=> $offset,
        										'blog' => $blog,
        										'array'=> true));
        	if(count($tempEvents) == 0)
        		break;//no more events to save
        	//print_r($tempEvents);
        	foreach($tempEvents as $tempEvent){
        		$EA = serialize($tempEvent->event_attributes);
        		print_r($EA);
        		if($EA){
        			foreach($conditions as $condition){
        				//echo $condition;
        				if(in_array("publish_to_university_homepage", $EA))
            				array_push($events, $tempEvent);
            		}
            	}
            }
            $offset += $grab;
        }
    
        //Trim off extra arrays, since we do 2 at a time
        return array_slice($events, 0, $limit);
    }

    Gives this as output (repeated multiple times):

    N;
    Warning: in_array() expects parameter 2 to be array, string given in /Users/thegreyspot/Workroom/wnmu/wp-content/themes/wnmu/lib/custom.php on line 166
  15. caimin_nwl
    NetWebLogic Support
    Posted 3 months ago #

    What are you getting for print_r($EA); ?

  16. thegreyspot
    Member
    Posted 2 months ago #

    Thats what the "N;" is... no idea what that means! haha

    [Moderator Note: No bumping, thank you.]

  17. thegreyspot
    Member
    Posted 2 months ago #

    Is there a way to use WP_Query() with Events manager? Because that would allow me to filter my custom fields natively. Cause, idk what else to do :/

  18. caimin_nwl
    NetWebLogic Support
    Posted 2 months ago #

    Yes, you should be able to do that:
    http://codex.wordpress.org/Class_Reference/WP_Query#Type_Parameters

    Events in EM are a custom post type called event.

  19. thegreyspot
    Member
    Posted 2 months ago #

    But would i be able to return the next nearest events coming up. And not the last posted date?

  20. thegreyspot
    Member
    Posted 2 months ago #

    I figured it out!!! $tempEvents->event_attributes wasn't working right because I was asking EM events to return an array rather than the Em events object! So i got rid of array=>true, and now I can do my correct filter.

    If anyone wants my final code:

    $events = getEventsWithFilter(10, array(array('field_name' => 'publish_to_university_homepage', 'field_value' => '1')));
    
    //Gets events, but also filters for certain custom fields. If any one of $condition fields are met, it will be return
    function getEventsWithFilter($limit, $conditions, $blog = ""){
    	$grab = 5;
    	$offset = 0;
    	$events = [];
        while(count($events) < $limit){
        	$tempEvents = EM_Events::get( array('limit' => $grab,
        										'offset'=> $offset,
        										'blog' => $blog
        										));
        	if(count($tempEvents) == 0)
        		break;//no more events to save
        	foreach($tempEvents as $tempEvent){
                $EA = $tempEvent->event_attributes;
        		if($EA){
        			foreach($conditions as $condition){
        				if($EA[$condition['field_name']] == $condition['field_value'])
            				$events[] = $tempEvent;
            		}
            	}
            }
            $offset += $grab;
        }
    
        //Trim off extra arrays, since we do 5 at a time
        return array_slice($events, 0, $limit);
    }

    PS. Thanks for all your help :)

Reply

You must log in to post.

About this Plugin

About this Topic

Tags