Site Reviews uses the WP_Query, so in order to do this, you will need to edit the “where” clause of the database query using the WordPress posts_where filter hook. You can do it like this:
/**
* Allow users to see their own pending reviews
* @param string $where
* @param \WP_Query $query
* @return string
*/
add_filter('posts_where', function ($where, $query) {
if ('site-review' !== $query->get('post_type') || !is_user_logged_in() || is_admin()) {
return $where;
}
global $wpdb;
$userId = get_current_user_id();
$table = $wpdb->prefix.'posts';
$search = "($table.post_status = 'publish')";
$replace = "($table.post_status = 'publish' OR ($table.post_author = '$userId' AND $table.post_status = 'pending'))";
return str_replace($search, $replace, $where);
}, 10, 2);
If you do this, you may also wish to visually display the review as a pending review. You can do this by using a custom template in your theme (see the Help page of the plugin to learn how to do this) in order to add a “pending” class which you can style yourself with some custom CSS.
Here is an example where we add the .glsr-status-publish
(approved) or .glsr-status-pending
(unapproved) class to the “review.php” template:
<?php defined('WPINC') || die; ?>
<div class="glsr-review glsr-status-<?= $review->status; ?>">
{{ title }}
{{ rating }}
{{ date }}
{{ assigned_to }}
{{ content }}
{{ avatar }}
{{ author }}
{{ response }}
</div>
Thank you for the code! Really appreciate your support!
I’ve put it into functions.php of the Theme Child and it is working (which I hope it is the right way to do this).
It is working for logged in users. But users will actually need an account for this which is not often the case since most review sites are public.
Is there a way to make it instead show up for users who just have written a review using a cookie or maybe using their IP address? or maybe there is an even simpler way?
This adds an INNER JOIN to the database query in order to use the IP Address in the WHERE clause of the query later on.
/**
* INNER JOIN the postmeta table in order to add the WHERE query for the _ip_address
* @param string $join
* @param \WP_Query $query
* @return string
*/
add_filter('posts_join', function ($join, $query) {
if ('site-review' !== $query->get('post_type') || is_admin()) {
return $join;
}
global $wpdb;
return $join." INNER JOIN {$wpdb->prefix}postmeta AS glsr_mt ON ({$wpdb->prefix}posts.ID = glsr_mt.post_id)";
}, 10, 2);
This filters the WHERE clause of the database query to include a check for the IP address.
/**
* Allow anyone to see their submitted pending reviews
* @param string $where
* @param \WP_Query $query
* @return string
*/
add_filter('posts_where', function ($where, $query) {
if ('site-review' !== $query->get('post_type') || is_admin()) {
return $where;
}
$ipAddress = function_exists('glsr')
? glsr('Helper')->getIpAddress()
: filter_input(INPUT_SERVER, 'REMOTE_ADDR');
if (empty($ipAddress)) {
return $where;
}
global $wpdb;
$table = $wpdb->prefix.'posts';
$search = "($table.post_status = 'publish')";
$replace = "($table.post_status = 'publish' OR (glsr_mt.meta_key = '_ip_address' AND glsr_mt.meta_value = '$ipAddress' AND $table.post_status = 'pending'))";
return str_replace($search, $replace, $where);
}, 10, 2);
Thank you very much for this extensive support! 🙂
Zee
(@laserjobs)
Did the reviews used to show the “pending” review to the user in the earlier versions?
I am not seeing the pending either after entering a review.
@laserjobs
No.
Pending (unapproved) reviews are not visible on your website until approved.