Skip Navigation

[Resolved] types_child_posts() not working as expected with php

This support ticket is created 6 years, 6 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 12 replies, has 2 voices.

Last updated by Beda 6 years, 6 months ago.

Assisted by: Beda.

Author
Posts
#955742

I am trying to:
Display related child posts types_child_posts() with PHP in single.php theme file. Projects (posts) are linked to CPT 'team' and CPT 'old-team'. This is the code:

			<dl class="credits">

				<?php
				$child_posts = types_child_posts("team");
				foreach ($child_posts as $child_post) 
				{ ?>
					<dt class="invisible credit"><?php _e( 'Team', 'html5blank' ); ?></dt>
				 	<dd><a href="<?php echo esc_url(get_permalink( $child_post->ID ) ); ?>"><?php echo $child_post->post_title; ?></a></dd> 
				<?php } ?>

				<?php
				$child_posts = types_child_posts("old-team");
				foreach ($child_posts as $child_post) 
				{ ?>
					<dt class="invisible credit"><?php _e( 'Team', 'html5blank' ); ?></dt>
				 	<dd><?php echo $child_post->post_title; ?></dd> 
				<?php } ?>

			</dl>

Link to a page where the issue can be seen:
hidden link

I expected to see:
The related posts from each CPT. In the case of the above example we should see 4 'team' related post (which are linked to the related post) and 1 'old-team' related post (which is not linked).

Instead, I got:
All related posts are displayed twice.

I have spent almost a whole day reading quite a lot of support pages, including finding several examples of the above code. This should be so simple but I just can't get it to work. Some help would be appreciated. ? Thanks!

#956386

I am not sure if you use a old Relationship, a Migrated Relationship or a new Relationship

It depends - usually I'd recommend to start already with the new API since that one is now public.
If this is legacy code you try to fix, I can have a look.

Otherwise I suggest to start with this:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/

There are some functions for legacy relationships as well as only for new relations.

Please let me know if that would solve the issue.
If you are working with this code since a while and wish to keep it I need to have a look, it may be necessary to port it.

#956392

Hi Beda,

Thanks for your quick reply. This website is just a few weeks old so I have used the latest plugin's for everything. So the relationships are completely new.

I have no idea is this is legacy code. On the toolset.com pages I have searched it is not clear if a code is legacy or not. The page you suggest I have studied many times but there are simply no useful examples, at least to me.

What do you mean with 'it may be necessary to port it.'? 🙂

#956424

Well, if this is a new site made with Types 3.0 you do need this API:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/

To get all related posts of a certain post, you can use https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts for example.

The examples are in the "More - Usage examples" section under each function.
Please let me know if you need particular help with.

You would need to do something like this:

toolset_get_related_posts(
    $the_current_post, // get posts related to this one, can be an ID, or current post $post->ID if you have the global $post
    'slug_of_the_relationship',//the slug of the post relationship you want to get the relation from
    'parent', // get posts where $the_current_post is the parent in given relationship
    $posts_per_page, $current_page, // pagination
    'post_object',//or the post_id
    'child'//you want the child posts
);

This above will return and array of posts that are connected to $the_current_post in a relationship as where $the_current_post is parent and the returned are child.

This works only in a relationship setup with Types 3.0 or above.

#962133

Thanks for your suggestion! I figured out how the code should be without giving errors:

				<?php
				$child_posts = toolset_get_related_posts(
					$post->ID, // get posts related to this one, can be an ID, or current post $post->ID if you have the global $post
					'team', //the slug of the post relationship you want to get the relation from
					array( 'team', 'project' ), // relationship between the posts
					'parent', // get posts where $the_current_post is the parent in given relationship
					'100', // pagination
					'0', // pagination
					array(),
					'post_object',//or the post_id
					'child'//you want the child posts
				);
				foreach ($child_posts as $child_post) 
				{ ?>
					<dt class="invisible credit"><?php _e( 'Team', 'html5blank' ); ?></dt>
				 	<dd><?php echo $child_post->post_title; ?></dd> 
				<?php } ?>

But it is not outputting any related posts. I just don't see why not. So I would still appreciate some help. Thanks!

#1069154

I do not think your Post Relationship Slug is "Team"

Rather, it should be something like any_type-any_other_type or similar.
Please head to Toolset > Relationships > Edit for getting the precise slug of the relationship

Further you are mixing old and new relationships API, I am not sure why.
As you mentioned this is a new relationship - hence, the example that uses array( 'team', 'project' ), // relationship between the posts doesn't work.
The array variant can be used only for legacy relationships (or those migrated from the legacy implementation, after new post relationships are enabled). The relationship slug variant will obviously work only if the new post relationships are already enabled.
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

Please use the Snippet provided here:
https://toolset.com/forums/topic/types_child_posts-not-working-as-expected-with-php/#post-956424

This should work.

#1069268

Thanks again! 🙂

I used the snippet you provided earlier but nothing I tried worked, it kept giving errors. What is very confusing is that Toolset is not following the regular 'WordPress' way of coding. A WordPress array would look like this:

array(
     'taxonomy' => 'category',
     'field'           => 'slug',
     'terms'        => array( 'quotes' ),
)

I think that's why so many people are having problems with the Toolset API. It's just very confusing. At least, I find it very confusing. 🙂 Anyway, your last comment did help me understand my mistake, which was about the 'slug_of_the_relationship'. I now understand what that is. 🙂 It's all working now as it should.

Thank you very much for your help, I really appreciate it! And for others, this is the complete code that did work:

<?php
$child_posts = toolset_get_related_posts(
    $post->ID,
    'project-teamlid',
    'parent',
    '100',
    '0',
    array(),
    'post_object'
    'child'
);
foreach ($child_posts as $child_post) 
{ ?>
    <dt class="invisible credit"><?php _e( 'Team', 'html5blank' ); ?></dt>
    <dd><?php echo $child_post->post_title; ?></dd> 
<?php } ?>

Please note: without the 'array(),' line, the code does not work.

#1069269

Sorry, I forgot to select that the problem is solved. 🙂

Have a great day,
Carsten

#1069484

Hi, I'm back. 🙂

I have now added arguments to select only if a custom field checkbox is not selected. This is how the code looks now (with an array! :-)):

				<?php
				$child_posts = toolset_get_related_posts(
				    $post->ID,
				    'project-teamlid',
				    'parent',
				    '100',
				    '0',
					array(
						'meta_key' => 'wpcf-oud-teamlid',
						'meta_compare' => 'LIKE',
						'meta_value' => ''
					),
				    'post_object',
				    'child'
				);
				foreach ($child_posts as $child_post) 
				{ ?>
				    <dt class="invisible credit"><?php _e( 'Team', 'html5blank' ); ?></dt>
				 	<dd><a href="<?php echo esc_url(get_permalink( $child_post->ID ) ); ?>"><?php echo $child_post->post_title; ?></a></dd> 
				<?php } ?>

But I get the following error:

WordPress databasefout: [Unknown column 'wpt0_postmeta_1' in 'where clause']
SELECT DISTINCT associations.child_id AS child_id FROM wpt0_toolset_associations AS associations JOIN wpt0_toolset_relationships AS relationships ON ( associations.relationship_id = relationships.id ) JOIN wpt0_posts AS wpt0_posts_1 ON (wpt0_posts_1.ID = parent_id) JOIN wpt0_posts AS wpt0_posts_2 ON (wpt0_posts_2.ID = child_id) LEFT JOIN wpt0_postmeta AS wpt0_postmeta_1 ON (wpt0_postmeta_1.post_id = child_id AND wpt0_postmeta_1.meta_key = 'wpcf-oud-teamlid') WHERE ( associations.relationship_id = 2 ) AND ( parent_id = 12455 ) AND ( wpt0_postmeta_1 LIKE '1' ) AND ( ( ( wpt0_posts_1.post_status IN ( 'publish', 'draft', 'pending', 'private' ) ) AND ( wpt0_posts_2.post_status IN ( 'publish', 'draft', 'pending', 'private' ) ) ) ) AND ( relationships.is_active = 1 ) LIMIT 100 OFFSET 0

Can you see what the problem is?

Thanks,
Carsten

#1070035

You cannot use meta key right now:
https://toolset.com/forums/topic/when-using-toolset_get_related_posts-and-trying-to-use-a-meta_compare-no-results/

It's broken and will be fixed in the next release.
The exact steps of that bug are:
1. Create a O2M Relationship of pages/posts (one page, many posts)
2. Add a Custom Single Line Field to the native posts
3. Create some native posts, complete the Single Line Field with some values and connect those posts to a page
4. Query the related Child posts within the Page template with toolset_get_related_posts()
(so to get all related child posts with a certain Custom Field value related to the current page)
The code for the page template:

$out = toolset_get_related_posts(
                get_the_ID(),//We are in the single page template so we get correct ID of current page here
                'page-post',//The relationship slug
                'parent',//that is the post type of get_the_ID()(we get posts related to this post in role parent)
                $limit = 100,//limit
                $offset = 0,//offset


                    //Here is the problem 
                $args = array('meta_key'=>'wpcf-toolset-single-line-on-posts','meta_value'=>'xy','meta_compare'=>'LIKE'),//We add a meta query by field single posts. Details: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

                  
                $return = 'post_id', //We want the related posts id returned
                $role_name_to_return = 'child',//return posts of child type
                $orderby = null,//order
                $order = 'ASC'//order
            );

            var_dump($out);

This will throw above mentioned error:

[Unknown column 'wp_postmeta_1' in 'where clause'] 
SELECT DISTINCT associations.child_id AS child_id FROM wp_toolset_associations AS associations JOIN wp_toolset_relationships AS relationships ON ( associations.relationship_id = relationships.id ) JOIN wp_posts AS wp_posts_1 ON (wp_posts_1.ID = parent_id) JOIN wp_posts AS wp_posts_2 ON (wp_posts_2.ID = child_id) LEFT JOIN wp_postmeta AS wp_postmeta_1 ON (wp_postmeta_1.post_id = child_id AND wp_postmeta_1.meta_key = 'wpcf-toolset-single-line-on-posts') WHERE ( associations.relationship_id = 1 ) AND ( parent_id = 2 ) AND ( wp_postmeta_1 LIKE 'xy' ) AND ( ( ( wp_posts_1.post_status IN ( 'publish', 'draft', 'pending', 'private' ) ) AND ( wp_posts_2.post_status IN ( 'publish', 'draft', 'pending', 'private' ) ) ) ) AND ( relationships.is_active = 1 ) LIMIT 100 OFFSET 0

Currently you should not use the meta query or wait for the fix release.
I pushed for a fast fix.

#1070055

Thanks, Beda.

This website is still in development so I do hope the new Toolset version is available for download within the next month.

Cheers,
Carsten

#1070062

This should be possible, yes, it's not a fixed ETA thou

#1078381