Advertisement
geminilabs

debug reviews

Nov 12th, 2020 (edited)
1,454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.69 KB | None | 0 0
  1. /**
  2.  * 1. This flushes the WordPress cache
  3.  * @see https://developer.wordpress.org/reference/functions/wp_cache_flush/
  4.  */
  5. add_action('init', function () {
  6.   wp_cache_flush();
  7. });
  8.  
  9. /**
  10.  * 2. Finally, here we manually query the first 10 reviews, review posts, and rating IDs
  11.  *    in order to confirm that the reviews are being queried correctly
  12.  */
  13. add_action('wp_footer', function () {
  14.   global $wpdb;
  15.   // get the 10 most recent (post <-> rating) IDs
  16.   $reviewIds = $wpdb->get_col("
  17.    SELECT r.review_id
  18.    FROM {$wpdb->prefix}glsr_ratings AS r
  19.    INNER JOIN {$wpdb->posts} AS p ON r.review_id = p.ID
  20.    WHERE 1=1 AND r.is_approved = 1 AND r.type = 'local'
  21.    GROUP BY r.review_id
  22.    ORDER BY r.is_pinned desc, p.post_date desc
  23.    LIMIT 10
  24.  ");
  25.   // get the 10 most recent reviews from the ratings table
  26.   $ratings = $wpdb->get_results("
  27.    SELECT r.*
  28.    FROM {$wpdb->prefix}glsr_ratings AS r
  29.    LIMIT 10
  30.  ");
  31.   // get the 10 most recent reviews from the posts table
  32.   $posts = $wpdb->get_results("
  33.    SELECT
  34.      ID,
  35.      post_author,
  36.      post_date,
  37.      post_date_gmt,
  38.      post_name,
  39.      post_title,
  40.      post_status,
  41.      post_modified,
  42.      post_modified_gmt,
  43.      menu_order
  44.    FROM {$wpdb->posts}
  45.    WHERE post_type = 'site-review'
  46.    AND post_status IN ('publish', 'pending')
  47.     ORDER BY post_date
  48.    LIMIT 10
  49.  ");
  50.   // Get the meta data of the first 10 review posts
  51.   $meta = [];
  52.   foreach ($posts as $post) {
  53.     $meta[$post->ID] = get_post_meta($post->ID);
  54.   }
  55.   // finally, log results to the console
  56.   glsr_log([
  57.     'review_ids' => $reviewIds,
  58.     'ratings' => $ratings,
  59.     'posts' => $posts,
  60.     'meta' => $meta,
  61.   ]);
  62. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement