Skip Navigation

[Résolu] wp_mail auto email with views only works for guest read permission in Access

This support ticket is created Il y a 4 années et 11 mois. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

Marqué : ,

Ce sujet contient 14 réponses, a 2 voix.

Dernière mise à jour par kaneB Il y a 4 années et 11 mois.

Assisté par: Luo Yang.

Auteur
Publications
#1422599

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.

#1423117

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.

#1425791

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.

#1425801

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

#1427073

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?

#1427209

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

#1427311
views screenshot.PNG

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.

#1427385

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

#1430789

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

#1430803

Try replacing the URL end with "....net2/login/". There is a plugin that bounce user off from admin URL. Sorry.

#1430963

Thanks for the details, I can log into your website, will update here if there is anything found

#1431205

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

#1432367

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]

#1432377

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:

if($views_id == 419){

With:

if( in_array($views_id, array(419, 123, 456, 789) ) ){

More help:
lien caché

#1432475

Very well, I could have a cloned view as a work around.

Issue solved, thanks a lot for the great guidance!