I am using the simple code suggested by LuoYang in this thread https://toolset.com/forums/topic/notification-setup-for-dynamic-expiration-date/ to send periodic emails from my site, doing a view shortcode. It is working fine until I manage the CPT permissions with Access. After setting the post type to be managed by Access, I tested and noted that only when read access is given to "Guest", the view will be displayed correctly. Any other higher access will result the View displaying "No Items Found" as defined in the wpv-no-items-found section.
Is there anything I can do to display the view correctly in emails without selecting guest, to avoid unauthorised access to the content?
My code:
[php]
// send automatic scheduled email
if (!wp_next_scheduled('pt_my_task_hook')) {
wp_schedule_event(time(), 'hourly', 'pt_my_task_hook');
}
add_action('pt_my_task_hook', 'pt_my_task_function');
function pt_my_task_function() {
wp_mail('email add', 'Test auto mail', do_shortcode('[wpv-view name="job-display-pending-jobs"]'), 'Content-Type: text/html; charset=UTF-8');
[/php]
Thanks.
Hello,
There isn't such kind of built-in feature within Toolset plugins, you might consider custom codes, for example:
1) add below PHP codes into your theme file functions.php:
add_filter('wpv_filter_query', function($query, $settings, $views_id){
// if it is doing a cron job then apply custom post_where filter
if($views_id == 123){
global $WP_Views;
$cron = 0;
foreach($WP_Views->view_shortcode_attributes as $k => $v){
if(isset($v['cron']) && $v['cron'] == 1){
$cron = 1;
}
}
if(!$cron)return;
add_filter('posts_where', 'my_func', 11);
}
return $query;
}, 99, 3);
function my_func($args){ // add "my-cpt" posts in query result
global $wpdb;
$args .= " OR $wpdb->posts.post_type = 'my-cpt'";
remove_filter('posts_where', 'my_func', 11);
return $args;
}
Please replace 123 with your view's ID (job-display-pending-jobs)
replace my-cpt with your custom post type slug
2) Edit the PHP codes you mentioned above, change this line from:
do_shortcode('[wpv-view name="job-display-pending-jobs"]'
To:
do_shortcode('[wpv-view name="job-display-pending-jobs" cron="1"]'
And test again.
I have spent hours (waiting for the mails) and test, here is what I found after using the code above:
-The mail will show all the 'job' post in that view in the mail, but the same view will not work on a page anymore. The original query filter in the view is also ignored (to filter out "pending jobs")
-I created a new view and nest the original views, result is similar to the above
-I replicated the original view into a new one, applied the same query filter and content output, and the mail will just display everything.
Seems like this code is overwriting the original view's query filter.
Yes, you are right, there is a mistake in above codes, please try to modify this line from:
if(!$cron)return;
add_filter('posts_where', 'my_func', 11);
With:
if($cron){
add_filter('posts_where', 'my_func', 11);
}
And test again
After the code amendment above, the shortcode can be used on a page as normal. However in the mail that it sends out, it again display all the posts there are, ignoring the query filter set within the view.
Any thoughts?
How do you setup the query filter set within the view?
Can you copy/paste them here? take some screenshots for your view, I need to test it in my localhost, thanks
My view is to display CPT "job" with query filter on a custom field Job Status (see screenshot).
With the code above the views working fine when displaying on a page but not in mail.
Since it is a custom codes problem, please provide a test site with the same problem, and fill below private message with login details, also point out the problem page URL and view URL, I need to test it in a live website. thanks
I have tried the credentials you provided above, it is not valid, I get this error:
sali**.com redirected you too many times.
Please check it, thanks
Try replacing the URL end with "....net2/login/". There is a plugin that bounce user off from admin URL. Sorry.
Thanks for the details, I can log into your website, will update here if there is anything found
I have done below modifications in your website, change the PHP codes in your theme file as below:
add_filter('wpv_filter_query', function($query, $settings, $views_id){
// if it is doing a cron job then apply custom post_where filter
if($views_id == 419){
global $WP_Views;
$cron = 0;
foreach($WP_Views->view_shortcode_attributes as $k => $v){
if(isset($v['cron']) && $v['cron'] == 1){
$cron = 1;
}
}
if($cron) {
remove_all_filters('posts_where');
}
}
return $query;
}, 99, 3);
Please test again, check if it is fixed. thanks
Great! This works now.
As I was testing with putting this view into another view (for some formatting requirement), it gives a blank output. Does this code not work for nested views?
Another question is, if I wish to use this feature for a few other views, how do I insert the view ID into this section?
[code]
if($views_id == 419){
global $WP_Views;
[/code]
No, above codes do not work for nested views or other complicated cases.
It is just a simple example codes for your reference.
For the question:
Another question is, if I wish to use this feature for a few other views, how do I insert the view ID into this section?
You can add more view's IDs, for example, replace this line from:
With:
if( in_array($views_id, array(419, 123, 456, 789) ) ){
More help:
hidden link
Very well, I could have a cloned view as a work around.
Issue solved, thanks a lot for the great guidance!