Skip Navigation

[Resolved] How to show in CRED Parent Selector only posts of logged in user?

This thread is resolved. Here is a description of the problem and solution.

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 6 years, 3 months ago. 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.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 25 replies, has 2 voices.

Last updated by Nigel 6 years, 3 months ago.

Assisted by: Nigel.

Author
Posts
#603782

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01: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.

#603793

Hi Nigel,

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 hidden link

#603809

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

OK, I'll take another look and get back to you.

I still have the site credentials in case I need them.

#605161

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Dave

Sorry for the delay getting back to you.

I've just taken a copy of your site so that I can install it locally to try and identify what the issue is, and I'll get back to you with what I find.

#605405

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01: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.

#605430

Hi Nigel,

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.

#605433

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01: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...

#606724

Hi Nigel, the only thing I do is placing the code into child functions.php.

#606772

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Can I get the debug info from you?

https://toolset.com/faq/provide-debug-information-faster-support/

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?

#606889

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. hidden link

PHP Notice:  Trying to get property of non-object in .../wp-includes/class-wp-query.php on line 3863

I haven´t seen it in backend or archive pages.

#607004

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Dave

That's odd, the warning messages show up in error_log.txt but not in debug.log ¯\_(ツ)_/¯

In any case, I did some digging, and the warning message shows up because of using is_page() in the pre_get_posts action.

It has been a known issue for some time and there are a number of WordPress trac tickets about this and related issues (e.g. https://core.trac.wordpress.org/ticket/21790).

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.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.