{"id":1536841,"date":"2018-05-23T02:17:46","date_gmt":"2018-05-23T09:17:46","guid":{"rendered":"https:\/\/theeventscalendar.com\/support\/forums\/topic\/wp_query-with-custom-meta_key\/"},"modified":"2018-05-25T00:06:54","modified_gmt":"2018-05-25T07:06:54","slug":"wp_query-with-custom-meta_key","status":"closed","type":"topic","link":"https:\/\/theeventscalendar.com\/support\/forums\/topic\/wp_query-with-custom-meta_key\/","title":{"rendered":"WP_Query with custom meta_key"},"content":{"rendered":"<p>Hi I am trying to do the following<\/p>\n<pre><code>$mp_wp_query = new WP_Query(\r\n\t\tarray(\r\n\t\t\t'posts_per_page'      =&gt; 10,\r\n\t\t\t'order_by'               =&gt; 'meta_value_num',\r\n\t\t\t'order'\t\t\t\t =&gt; 'DESC',\r\n\t\t\t'post_status'         =&gt; 'publish',\r\n\t\t\t'post_type' =&gt; 'tribe_events',\r\n\t\t\t'meta_key' =&gt; 'wpb_post_views_count',\r\n\t\t)\r\n\t);<\/code><\/pre>\n<p>I am trying to get the events ordered by page views which is stored in wp_postmeta as wpb_post_views_count<\/p>\n<p>However I am not getting the results I expected and looking at the SQL created and the results of that SQL there is a strange behavior happening<\/p>\n<p>The SQL genertated is<\/p>\n<p><code>SELECT SQL_CALC_FOUND_ROWS DISTINCT wp_posts.*, MIN(wp_postmeta.meta_value) as EventStartDate, MIN(tribe_event_end_date.meta_value) as EventEndDate FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id ) LEFT JOIN wp_postmeta as tribe_event_end_date ON ( wp_posts.ID = tribe_event_end_date.post_id AND tribe_event_end_date.meta_key = '_EventEndDate' ) WHERE 1=1 AND ( wp_postmeta.meta_key = 'wpb_post_views_count' AND ( mt1.meta_key = '_EventStartDate' ) ) AND wp_posts.post_type = 'tribe_events' AND ((wp_posts.post_status = 'publish')) AND (wp_postmeta.meta_value &gt;= '2018-05-23 00:20:00' OR (wp_postmeta.meta_value &lt;= '2018-05-23 00:20:00' AND tribe_event_end_date.meta_value &gt;= '2018-05-23 00:20:00' )) GROUP BY wp_posts.ID ORDER BY EventStartDate DESC, wp_posts.post_date DESC LIMIT 0, 10<\/code><\/p>\n<p>And a sql result will have a strange <strong>StartDate <\/strong>Here is an example<\/p>\n<pre><code>EventStartDate,EventEndDate\r\n\"1\",\"2018-05-25 10:40:00\"<\/code><\/pre>\n<p>I think this something to do with<\/p>\n<p><code>MIN(wp_postmeta.meta_value) as EventStartDate<\/code><\/p>\n<p>As the postmeta minumum value for this event is <code>1<\/code> i&#8217;m not very good with SQL to understand where the problem is.<\/p>\n<p>Can you help?<\/p>\n<p>Thanks.<\/p>\n","protected":false},"template":"","class_list":["post-1536841","topic","type-topic","status-closed","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>WP_Query with custom meta_key -<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/theeventscalendar.com\/support\/forums\/topic\/wp_query-with-custom-meta_key\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"WP_Query with custom meta_key -\" \/>\n<meta property=\"og:description\" content=\"Hi I am trying to do the following $mp_wp_query = new WP_Query( array( &#039;posts_per_page&#039; =&gt; 10, &#039;order_by&#039; =&gt; &#039;meta_value_num&#039;, &#039;order&#039; =&gt; &#039;DESC&#039;, &#039;post_status&#039; =&gt; &#039;publish&#039;, &#039;post_type&#039; =&gt; &#039;tribe_events&#039;, &#039;meta_key&#039; =&gt; &#039;wpb_post_views_count&#039;, ) ); I am trying to get the events ordered by page views which is stored in wp_postmeta as wpb_post_views_count However I am not [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/theeventscalendar.com\/support\/forums\/topic\/wp_query-with-custom-meta_key\/\" \/>\n<meta property=\"article:modified_time\" content=\"2018-05-25T07:06:54+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/theeventscalendar.com\/support\/forums\/topic\/wp_query-with-custom-meta_key\/\",\"url\":\"https:\/\/theeventscalendar.com\/support\/forums\/topic\/wp_query-with-custom-meta_key\/\",\"name\":\"WP_Query with custom meta_key -\",\"isPartOf\":{\"@id\":\"https:\/\/theeventscalendar.com\/support\/#website\"},\"datePublished\":\"2018-05-23T09:17:46+00:00\",\"dateModified\":\"2018-05-25T07:06:54+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/theeventscalendar.com\/support\/forums\/topic\/wp_query-with-custom-meta_key\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/theeventscalendar.com\/support\/forums\/topic\/wp_query-with-custom-meta_key\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/theeventscalendar.com\/support\/forums\/topic\/wp_query-with-custom-meta_key\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/theeventscalendar.com\/support\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Topics\",\"item\":\"https:\/\/theeventscalendar.com\/support\/topics\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Calendar Products\",\"item\":\"https:\/\/theeventscalendar.com\/support\/forums\/forum\/events\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Events Calendar PRO\",\"item\":\"https:\/\/theeventscalendar.com\/support\/forums\/forum\/events\/events-calendar-pro\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"WP_Query with custom meta_key\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/theeventscalendar.com\/support\/#website\",\"url\":\"https:\/\/theeventscalendar.com\/support\/\",\"name\":\"\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/theeventscalendar.com\/support\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"WP_Query with custom meta_key -","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/theeventscalendar.com\/support\/forums\/topic\/wp_query-with-custom-meta_key\/","og_locale":"en_US","og_type":"article","og_title":"WP_Query with custom meta_key -","og_description":"Hi I am trying to do the following $mp_wp_query = new WP_Query( array( 'posts_per_page' =&gt; 10, 'order_by' =&gt; 'meta_value_num', 'order' =&gt; 'DESC', 'post_status' =&gt; 'publish', 'post_type' =&gt; 'tribe_events', 'meta_key' =&gt; 'wpb_post_views_count', ) ); I am trying to get the events ordered by page views which is stored in wp_postmeta as wpb_post_views_count However I am not [&hellip;]","og_url":"https:\/\/theeventscalendar.com\/support\/forums\/topic\/wp_query-with-custom-meta_key\/","article_modified_time":"2018-05-25T07:06:54+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/theeventscalendar.com\/support\/forums\/topic\/wp_query-with-custom-meta_key\/","url":"https:\/\/theeventscalendar.com\/support\/forums\/topic\/wp_query-with-custom-meta_key\/","name":"WP_Query with custom meta_key -","isPartOf":{"@id":"https:\/\/theeventscalendar.com\/support\/#website"},"datePublished":"2018-05-23T09:17:46+00:00","dateModified":"2018-05-25T07:06:54+00:00","breadcrumb":{"@id":"https:\/\/theeventscalendar.com\/support\/forums\/topic\/wp_query-with-custom-meta_key\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/theeventscalendar.com\/support\/forums\/topic\/wp_query-with-custom-meta_key\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/theeventscalendar.com\/support\/forums\/topic\/wp_query-with-custom-meta_key\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/theeventscalendar.com\/support\/"},{"@type":"ListItem","position":2,"name":"Topics","item":"https:\/\/theeventscalendar.com\/support\/topics\/"},{"@type":"ListItem","position":3,"name":"Calendar Products","item":"https:\/\/theeventscalendar.com\/support\/forums\/forum\/events\/"},{"@type":"ListItem","position":4,"name":"Events Calendar PRO","item":"https:\/\/theeventscalendar.com\/support\/forums\/forum\/events\/events-calendar-pro\/"},{"@type":"ListItem","position":5,"name":"WP_Query with custom meta_key"}]},{"@type":"WebSite","@id":"https:\/\/theeventscalendar.com\/support\/#website","url":"https:\/\/theeventscalendar.com\/support\/","name":"","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/theeventscalendar.com\/support\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/theeventscalendar.com\/support\/wp-json\/wp\/v2\/topic\/1536841","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theeventscalendar.com\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/theeventscalendar.com\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":2,"href":"https:\/\/theeventscalendar.com\/support\/wp-json\/wp\/v2\/topic\/1536841\/revisions"}],"predecessor-version":[{"id":1538512,"href":"https:\/\/theeventscalendar.com\/support\/wp-json\/wp\/v2\/topic\/1536841\/revisions\/1538512"}],"wp:attachment":[{"href":"https:\/\/theeventscalendar.com\/support\/wp-json\/wp\/v2\/media?parent=1536841"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}