WP_Query with custom meta_key

Home Forums Calendar Products Events Calendar PRO WP_Query with custom meta_key

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1536841
    hypersrv
    Participant

    Hi I am trying to do the following

    $mp_wp_query = new WP_Query(
    		array(
    			'posts_per_page'      => 10,
    			'order_by'               => 'meta_value_num',
    			'order'				 => 'DESC',
    			'post_status'         => 'publish',
    			'post_type' => 'tribe_events',
    			'meta_key' => '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 getting the results I expected and looking at the SQL created and the results of that SQL there is a strange behavior happening

    The SQL genertated is

    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 >= '2018-05-23 00:20:00' OR (wp_postmeta.meta_value <= '2018-05-23 00:20:00' AND tribe_event_end_date.meta_value >= '2018-05-23 00:20:00' )) GROUP BY wp_posts.ID ORDER BY EventStartDate DESC, wp_posts.post_date DESC LIMIT 0, 10

    And a sql result will have a strange StartDate Here is an example

    EventStartDate,EventEndDate
    "1","2018-05-25 10:40:00"

    I think this something to do with

    MIN(wp_postmeta.meta_value) as EventStartDate

    As the postmeta minumum value for this event is 1 i’m not very good with SQL to understand where the problem is.

    Can you help?

    Thanks.

    #1538511
    Jennifer
    Keymaster

    Hello,

    Thanks for reaching out!

    First to set expectations, custom coding is outside the scope of support that we are able to provide here in the forums. However, I tested out your code on my end but was not seeing the same thing with the start date. I will run this by our developers to see if they can provide you with any insight here and let you know as soon as I hear back.

    Thanks,

    Jennifer

    #1541926
    Jennifer
    Keymaster

    Hello,

    One of our developers recommended adding this to your functions.php file to check if you still get the number 1 instead of the event start date:

    $the_query = new WP_Query( array(
    'posts_per_page' => 10,
    'order_by' => 'meta_value_num',
    'order' => 'DESC',
    'post_status' => 'publish',
    'post_type' => 'tribe_events',
    'meta_key' => 'wpb_post_views_count',
    ) );

    // The Loop
    if ( $the_query->have_posts() ) {
    echo '

      ';
      while ( $the_query->have_posts() ) {
      $the_query->the_post();
      echo '

    • ' . get_the_title() . '
    • ';
      echo '

    • ' . tribe_get_start_date() . '
    • ';
      echo '

    • ' . tribe_get_end_date() . '
    • ';
      }
      echo '

    ';
    /* Restore original Post Data */
    wp_reset_postdata();
    } else {
    // no posts found
    }

    Since your query looks good on our end, this will let us know if, for some reason, the wrong value is saved on _EventStartDate.

    #1558396
    Support Droid
    Keymaster

    Hey there! This thread has been pretty quiet for the last three weeks, so we’re going to go ahead and close it to avoid confusion with other topics. If you’re still looking for help with this, please do open a new thread, reference this one and we’d be more than happy to continue the conversation over there.

    Thanks so much!
    The Events Calendar Support Team

Viewing 4 posts - 1 through 4 (of 4 total)
  • The topic ‘WP_Query with custom meta_key’ is closed to new replies.