Skip to content

Instantly share code, notes, and snippets.

@craigfrancis
Created March 22, 2022 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save craigfrancis/9230be877423e73a28475b061495fee7 to your computer and use it in GitHub Desktop.
Save craigfrancis/9230be877423e73a28475b061495fee7 to your computer and use it in GitHub Desktop.
<?php
global $wpdb;
$table = _get_meta_table( 'post' );
// add_metadata( 'post', 1, 'MyCustomField1', 'MyCustomValue1' );
// for ($k = 0; $k < 100; $k++) {
// $values = [];
// $values[] = '( ' . $k . ', "pinplugin_event_start_date", "2020-01-20")';
// $values[] = '( ' . $k . ', "pinplugin_event_start_time", "10:00:00")';
// $values[] = '( ' . $k . ', "pinplugin_event_end_time", "20:00:00")';
// for ($j = 0; $j < 30; $j++) {
// $values[] = '( ' . $k . ', "MyCustomField' . $j . '", "MyCustomValue' . $j . '")';
// }
// $wpdb->query( 'INSERT INTO ' . $table . ' ( post_id, meta_key, meta_value ) VALUES ' . implode(', ', $values) );
// }
// exit('Done');
$c1 = hrtime(true);
$query_args = [
'relation' => 'OR',
[
'key' => 'pinplugin_event_start_date',
'value' => '2020-01-23',
'type' => 'DATE',
'compare' => '<'
],
[
'relation' => 'AND',
[
'key' => 'pinplugin_event_start_date',
'value' => '2020-01-23',
'type' => 'DATE',
'compare' => '='
],
[
'key' => 'pinplugin_event_start_time',
],
[
'key' => 'pinplugin_event_end_time',
'value' => '17:18:05',
'type' => 'TIME',
'compare' => '<='
],
]
];
$meta_query = new WP_Meta_Query( $query_args );
$meta_query_sql = $meta_query->get_sql('post', $wpdb->posts, 'ID', null);
$sql = '
SELECT
wp_posts.ID
FROM
' . $wpdb->posts . '
' . $meta_query_sql['join'] . '
WHERE
1=1
' . $meta_query_sql['where'] . '
GROUP BY
wp_posts.ID';
$results = $wpdb->get_results($sql);
$c2 = hrtime(true);
echo round((($c2 - $c1)/1000000000), 4) . "\n";
echo count($results) . "\n\n";
echo $sql . "\n\n";
exit();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment