[Résolu] How to show in CRED Parent Selector only posts of logged in user?
Ce fil est résolu. Voici une description du problème et la solution proposée.
Problem:
A CRED form includes a selector to choose a related parent post. This field lists *all* such parent posts. Client wants it to only show parent posts authored by the current user (i.e. their own parent posts).
Solution:
This thread describes a custom code solution, but this is a common enough request and this feature will be added natively in an upcoming release and it is recommended to wait for that.
This support ticket is created Il y a 6 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.
Les langues: Anglais (English )Espagnol (Español )
Fuseau horaire: Europe/London (GMT+00:00)
Hi Dave
Is it only after the last update—adding the ajax test—that those problems appeared?
If you revert to the previous version do they still happen?
The conditions I have proposed may not target the individual queries effectively enough, and I'm not sure that I am able to accurately target the ajax query in particular, I can take another look but it may be that using select2 while adding the code to show only the user's own parent posts may need to be a feature request.
it comes also from the first code you provided me, but I didn´t look to homepage before where it is displayed so I have found this error yesterday. I placed this code into funtions.php so you can see the error on lien caché
Les langues: Anglais (English )Espagnol (Español )
Fuseau horaire: Europe/London (GMT+00:00)
Hi Dave
On my local copy of your site I tested both the simplified version I initially gave you and the version that I provided that also works with ajax, i.e. this one:
/**
* Limit CRED parent post selector to own posts
*/
function filter_parent_posts( $query ){
$target_page = 981; // ID of page with CRED form, or string of page slug
$parent_post_type = "zazitek"; // slug of parent post type
$child_post_type = "varianta-zazitku"; // slug of child post type
if ( ( is_page( $target_page ) || is_singular( $child_post_type ) || ( defined('DOING_AJAX') && DOING_AJAX ) ) && $query->get( 'post_type') == $parent_post_type ) {
$current_user = get_current_user_id();
$query->set( 'author', $current_user );
}
}
add_action( 'pre_get_posts', 'filter_parent_posts' );
After installing the site I activated all of the Toolset plugins, nothing else, and then ran tests to check that the code worked as expected.
With each test I also loaded the home page, which you said was producing PHP errors when the code is active.
The code worked as expected, including when using select2 and updating the parent list via ajax, and there were no errors or warnings at any time in the debug.log.
It could be that there is some conflict with one of your other plugins, but as this is custom code that is not something I can debug. If you do some testing and identify a plugin which conflicts with this code sample I'd be curious to know which.
I have updated all the Toolset plugins to the latest versions as well as Divi Theme, tried to deactivate all other plugins and the problem is still there. I have also edited the Child theme to the default state and added your latest function, but this also does not work.
Les langues: Anglais (English )Espagnol (Español )
Fuseau horaire: Europe/London (GMT+00:00)
Can you give me exact steps to reproduce the errors, because they don't appear on my copy of your site, even if I go ahead and activate the other plugins...
And, to confirm, you see the PHP message in your debug.log when adding the code I gave you and, what?, visiting any page on your site, front-end or back-end?
Hi, the error is not in debug.log, but I found it in error_log.txt. If I remove the function, nothing appears in log, but if I add the function and I visit the homepage, it will show 4 notices:
PHP Notice: Trying to get property of non-object in .../wp-includes/class-wp-query.php on line 3728
PHP Notice: Trying to get property of non-object in .../wp-includes/class-wp-query.php on line 3730
PHP Notice: Trying to get property of non-object in .../wp-includes/class-wp-query.php on line 3732
PHP Notice: Trying to get property of non-object in .../wp-includes/class-wp-query.php on line 3863
And 1 notice is displayed when I want to show any post type, e. g. lien caché
PHP Notice: Trying to get property of non-object in .../wp-includes/class-wp-query.php on line 3863
The long and the short of it is that the code works as written, but is generating those warnings, which don't affect anything else.
It may be possible to rewrite the code so that it still functions as required without generating the warnings, but I'll leave that to you if you are worried about it.