Skip Navigation

[Resolved] How to Create post relationship between custom post types

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.

This topic contains 33 replies, has 2 voices.

Last updated by Minesh 4 years, 1 month ago.

Assigned support staff: Minesh.

Author
Posts
#540420

i just hid harmonogramy form admin menu:) because i just want to use it as connector dont want to client admin use it or change something i want him to add created DNI diety while creating DIETY UZYTKOWNIKA it means in english i just want admin to add diet days while creating whole diet for a Given user and only this user can see the whole diet but those days created can be used in different combinations in other diets:) so private for user must be this DIETA UZYTKOWNIKA and whole plan (HARMONOGRAM) and would be best not to direct access to days (DNI DIETY) but those days

once again testing credentials:

test/Test1982$

#540490

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - As I understand:

You are using this layout to display 'Diety użytkowników' and inside that you have added the view which displays the child of 'Diety użytkowników'.

Now - When I see your setup its looks like the relationship is corrupted.

If you visit the following link - your are editing the post with ID 34:
=> hidden link

Now - scroll down to relationship box - and if you edit the post its displaying the same post with ID 34. Why?

First of all, I would recommend creating post type from scratch - build the relationship (do not hide any post type on this stage) - add few related data - make sure you build the relationship without any issue and then tell me to check post author issue.

#540509

ok will delete all post types and create again i had one situation that the checkbox has gone in harmongram ( parent child relationship) i chose it again and saved . But i am a little woried about future how it happened that two posts have the same id? Once you create parent child relationship you can not change it in post type settings?

#540557

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Once you create parent child relationship you can not change it in post type settings?
=> Correct - otherwise you will get unexpected results.

Once you generate content for parent/child relationship you should not change it.

#541477

ok i deleted all created once more and now i have two testing diets:

hidden link

hidden link
Now both users can see both diets we have to change something in views

I noticed that author of added child items (harmonogram) is admin despite the fact parent is TEST so how to make it automatically set the author - this is one way and then add in view condition to filter by author and logged user or second add just the condition if parent item is the same as logged user someone can see diet days (child items no matter who is author) if parent diet author is not the same as logged user he can not see parent item and child items

Can you please look at the view for displaying nested harmonograms?

#541879

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - to automatically set the post author for ALL child posts equal to parent post author you need to add custom code to your current theme's functions.php file.

Please note that to give support on custom programming is beyond the scope of our support policy BUT I will guide you in right direction.

I suggest you should try with "save_post" action hook and using "wp_update_post" function update the title.
=> http://codex.wordpress.org/Plugin_API/Action_Reference/save_post
=> https://codex.wordpress.org/Function_Reference/wp_update_post
=> https://stackoverflow.com/questions/20444690/change-post-author-on-post-update-in-wordpress

For example:

function func_auto_update_post_author( $post_id ) { 
    $post_type = get_post_type($post_id);
    
if ( "harmonogram" != $post_type ) return;
  
         $my_post = array(
            'ID'            => $post_id,
            'post_author'   => get_current_user_id(),  /// SET your PARENT POST AUTHOR ID
        );

        remove_action('save_post', 'update_venue_concert');
        wp_update_post($my_post);
        add_action( 'save_post', 'update_venue_concert');
}
add_action( 'save_post', 'func_auto_update_post_author');

You need to adjust above code to set parent post author when creating child posts (harmonogram).

#541939

ok and then it is enough to set author filter to restrict access? How to restrict access to URL of a user diet?I think filters only content but still i can acccess another user url like admin can access url of test and the other way round

#541965

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - as per the WordPress roles and capabilities ADMIN has rights to access anything.
=> https://codex.wordpress.org/Roles_and_Capabilities

However - to restrict the user with read permission on frontend you can create post groups:
=> https://toolset.com/documentation/user-guides/limiting-read-access-specific-content/

So first I would ask you to setup your content according to your requirement and then create a new ticket for your each new question as this ticket is going on and on.

Also - using access, you can assign/remove capabilities to entire POST TYPE to specific roles, not to specific post.

So - this will need custom programming and that is eventually beyond the scope of our support policy but I will try to guide you in right direction BUT first thing is that you should setup your site with content.

#541988

ok i will setup content with 2 normal users and 2 diets - i know i can restrict group access and role but those users will have the same roles so i need to restrict acces by author or by user id

#541993

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok - please setup your content first and then I will try to setup permission per user. Please note that it will involve custom programming and I will try my level best to help you but if its beyond the scope I will just give you direction and you need to contact our certified partners:
=> https://toolset.com/consultant/

#542623

I did the code like this now saving each harmonogram it has its parent dieta author can you confirm everything about geting parent id and author is ok 🙂

function func_auto_update_post_author( $post_id ) { 
    $post_type = get_post_type($post_id);
     
if ( "harmonogram" != $post_type ) return;
   
$parent_id = wpcf_pr_post_get_belongs($post_id, 'dieta');
if (!empty($parent_id)) {
$parent = get_post($parent_id);
}

   $post_author_id = get_post_field( 'post_author',  $parent );
         $my_post = array(
            'ID'            => $post_id,
            'post_author'   => $post_author_id,  /// SET your PARENT POST AUTHOR ID
        );
 
        remove_action('save_post', 'func_auto_update_post_author');
        wp_update_post($my_post);
        add_action( 'save_post', 'func_auto_update_post_author');
}
add_action( 'save_post', 'func_auto_update_post_author');

#542892

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - if you create few entries and if that works that means your code is OK and working. Did you try to create child entries and check if correct parent author is set for child entries as per parent author.

Do you see it assigns - correct author?

I've added query filter - filter post by post author as logged in user:

Wybierz wpisy, których author jest tą samą osobą, co aktualnie zalogowany użytkownik.

And I can see that it disaplys correct count.

Also, I do not see way to check/debug the code you added as I cant able to access functions.php file. Could you please send me FTP access details. The FTP details you sent to me before is working but I do not able to access any folter or file.

#542921

ok i created two users one the same as before test and second one test2 :

hidden link
hidden link

view only author items filter and it seems to work maybe but what to do to restrict author ?diet=dieta-test page now there is no content as author filter is working but i want to restrict whole url Layout for single "dieta" to user or redirect to my account or something

i will check the ftp credentials and edit this post

#542926

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Great - what if you try to use WordPress hook template_redirect .

The following code will work when any user will be logged in using role having author or customer.

For example:

global $current_user;
if(in_array('author',$current_user->roles) or in_array('customer',$current_user->roles) ){
	add_action( 'template_redirect', 'func_custom_redirect' );
}
function func_custom_redirect() {
    global $post,$user_ID, $post_type;
	
	if(is_author(get_current_user_id()) {
                wp_redirect("/");
                 exit;
        }
}

More info:
=> https://codex.wordpress.org/Plugin_API/Action_Reference/template_redirect

I hope above solution work for you.

#543011

It is not working:( .

I have another question as you know the views and layouts in my system. You can of course move it to a new ticket.

I have a custom date in harmonogram and i would like to make a list of those dates (it easy i already created view) but they should link not to harmonogram item but shoud be connected with pagination as you can see in diets there is a paginatation but we still are in the same view on a diet page i dont want to redirect to given harmonogram rather stay in the same view but switch page but pagination items shouldnt be numbers but this custom field:

hidden link
hidden link