{"id":7321156,"date":"2016-04-26T15:24:15","date_gmt":"2016-04-26T15:24:15","guid":{"rendered":"https:\/\/wordpress.org\/support\/topic\/custom-scope-filter-not-works\/"},"modified":"2016-08-31T16:56:39","modified_gmt":"2016-08-31T16:56:39","slug":"custom-scope-filter-not-works","status":"publish","type":"topic","link":"https:\/\/wordpress.org\/support\/topic\/custom-scope-filter-not-works\/","title":{"rendered":"Custom Scope Filter not works"},"content":{"rendered":"<p>Hi guys,<br \/>\nI need to create a <strong>custom scope filter<\/strong>:<\/p>\n<p>I would like to have <strong>all events from 6am of today to 6am of tomorrow<\/strong>.<\/p>\n<p>So if today is 2016-04-26, I&#8217;d like to have all events from the 6am of 2016-04-26 to midnight of 2016-04-26 plus event from midnight of 2016-04-27 to 6am of 2016-04-27.<\/p>\n<p>Here my code:<\/p>\n<pre><code>add_filter( &#039;em_events_build_sql_conditions&#039;, &#039;custom_today_scope_conditions&#039;,1,2);\nfunction custom_today_scope_conditions($conditions, $args){\n\tif( !empty($args[&#039;scope&#039;]) &amp;&amp; $args[&#039;scope&#039;]==&#039;today-custom&#039; ){\n\t\t$today_date = date(&#039;Y-m-d&#039;,current_time(&#039;timestamp&#039;));\n\t\t$tomorrow_date = date(&#039;Y-m-d&#039;,strtotime(&quot;+1 day&quot;, current_time(&#039;timestamp&#039;)));\n\t\t$conditions[&#039;scope&#039;] = &quot; (event_start_date = CAST(&#039;$today_date&#039; AS DATE) and EXTRACT(HOUR FROM event_start_time) &gt; 6 ) OR (event_start_date = CAST(&#039;$tomorrow_date&#039; AS DATE) and EXTRACT(HOUR FROM event_start_time) &lt; 6 )&quot;;\n\t}\n\treturn $conditions;\n}\n\nadd_filter( &#039;em_get_scopes&#039;,&#039;my_em_scopes&#039;,1,1);\nfunction my_em_scopes($scopes){\n\t$my_scopes = array(\n\t\t&#039;today-custom&#039; =&gt; &#039;Today Custom&#039;\n\t);\n\treturn $scopes + $my_scopes;\n}<\/code><\/pre>\n<p>and my <strong> working query<\/strong> (tested on phpMyAdmin):<\/p>\n<p><code>SELECT * FROM &quot;SqE_em_events&quot; WHERE (&quot;event_start_date&quot;= &#039;2016-04-26&#039; and EXTRACT(hour FROM &quot;event_start_time&quot;) &gt; 6 ) or (&quot;event_start_date&quot; = &#039;2016-04-27&#039; and EXTRACT(hour FROM &quot;event_start_time&quot;) &lt; 6 )<\/code><br \/>\n( you have to substituite &#8221; with ` )<\/p>\n<p>Thank you very much for any help<\/p>\n<p>https:\/\/wordpress.org\/plugins\/events-manager\/<\/p>\n","protected":false},"template":"","class_list":["post-7321156","topic","type-topic","status-publish","hentry"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/7321156","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/7321156\/revisions"}],"wp:attachment":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=7321156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}