Skip Navigation

[Resuelto] New Post Relatsionships API – ‘call to undefined function’

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

Problem:
The new Post Relationships API has landed, very happy to see this:

https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/

Details

CPTs slugs
music-release (Music Release)
music-store (Music Store)

Relationship slug
music-release-music-store

Intermediary CPT slug
music-release-music

Intermediate CPT custom field slug
wpcf-purchase-link

So on my Music Release CPT theme template I want to:

1. Display the purchase link from the intermediary cpt that links the Music Release and the Music Store
2. Display the logo from the Music Store CPT linked to in the relationship, through the intermediary (music-release-music).

Solution:

Types 2.3-b3 (beta 3) indeed didn't include the new public API, it was released only yesterday with 2.3-b4, you can download it here:
https://toolset.com/account/downloads/
Choose channel: Beta

- originally, it was intended to be released both 2.3-b4 and 2.2.22 at the same time, but there have been delays (glitches on the part of the beta version)

And you can use function toolset_get_related_posts(), like this:

$intermediary_posts = toolset_get_related_posts( 
    get_the_ID(), //Post to query by.
    'music-release-music-store',  //Slug of the relationship to query by 
    'parent', //Name of the element role to query by.
    100, //Maximum number of returned results
    0, //Result offset
    array(),//Additional query arguments
    'post_id', //Determines return type
    'intermediary' // which posts from the relationship should be returned
);

Relevant Documentation:

https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/

This support ticket is created hace 6 años, 9 meses. 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.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

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)

Etiquetado: 

Este tema contiene 16 respuestas, tiene 2 mensajes.

Última actualización por Geoffrey Cleverley hace 6 años, 8 meses.

Asistido por: Luo Yang.

Autor
Mensajes
#621296

Hey there,

So... the new Post Relationships API has landed, very happy to see this.
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/

Having a little problem though.

Details

CPTs slugs
music-release (Music Release)
music-store (Music Store)

Relationship slug
music-release-music-store

Intermediary CPT slug
music-release-music

Intermediate CPT custom field slug
wpcf-purchase-link

So on my Music Release CPT theme template I want to:

1. Display the purchase link from the intermediary cpt that links the Music Release and the Music Store
2. Display the logo from the Music Store CPT linked to in the relationship, through the intermediary (music-release-music).

Part 1 I have achieved with the new API, as follows:

$query_purchase_intermediary = new WP_Query(
	array(
		'post_type' => 'music-release-music',
		'numberposts' => -1,
		'toolset_relationships' => array(
			array(
				'role' => 'child',
				'related_to' => get_the_ID(),
				'relationship' => array( 'music-release', 'music-release-music' )
			),
			array(
				'role' => 'parent',
				'related_to' => $selected_post,
				'relationship' => 'music-release-music-store'
			),
		),
		'meta_key' => 'wpcf-purchase-link',
		'orderby' => 'meta_value',
		'order' => 'ASC',
	)
);

$music_release_purchase_intermediary_posts = $query_purchase_intermediary->posts;

foreach ( $music_release_purchase_intermediary_posts as $music_release_purchase_intermediary_post ) {
	$purchase_link = get_post_meta( $music_release_purchase_intermediary_post->ID, 'wpcf-purchase-link', true );
	echo $purchase_link;

        // In here put code to query the parent Music Store of the Intermediary
       // Get the Music Store Thumbnail to display and any other information.
}

So I can display the purchase link from the intermediary post type in the relationship between the Music Release and the Music Store no problem.

However now I want to display the Logo Image (thumbnail) from the Music Store on the Music Release Page.

How would I do that?

Within the foreach loop above, I have tried to get the relationship linked Music Store post object in the following ways:

$store = toolset_get_parent_post_by_type(  $music_release_purchase_intermediary_post, 'music-store' );
$store = toolset_get_parent_post_by_type(  $music_release_purchase_intermediary_post->ID, 'music-store' );
$store = toolset_get_related_posts(
	$music_release_purchase_intermediary_post,
	array( 'music-store', 'music-release-music' ),
	'child',
	$posts_per_page,
	$current_page,
	array(),
	'post_object'
);

However, I am getting a 'call to undefined function' error for each of these new API functions.

When i am typing them out in PHPStorm, I am also not getting any function autocorrect in the list of toolset functions.

Any ideas what is the problem?

Should I be using another WP_Query within the foreach loop?

Thanks

Jeff

#621401

For the second toolset function of the three I tried above, I didn't do:

$store = toolset_get_parent_post_by_type(  $music_release_purchase_intermediary_post->ID, 'music-store' );

I did

$store = toolset_get_related_post( $music_release_purchase_intermediary_post->ID, array( 'music-store', 'music-release-music' ) );

Jeff

#621413

Hahaha Oh dear, I tell a lie...

My query that I thought is working, is not...

It returns every purchase link from every intermediate of every store and release....

#621463

What I would like is this, if possible please.

cpt slug = store
cpt slug = product

relationship slug = store-product
intermediary slug = store-product

(types sets the relationship and the intermediary slug to the same as default, so lets stick with that.)

intermediary custom field slug = wpcf-price

Query to display price (wpcf-price) from the intermediary, on the product page. From the relationship linking to each product to each store.

Further query to be able to show each store title on the product page.

Looking forward to working this out.

Thanks

Jeff

#621938

Dear Jeff,

For your last quesion:

Query to display price (wpcf-price) from the intermediary, on the product page. From the relationship linking to each product to each store.

Since it is a custom PHP codes problem, I am not sure how do you setup the post type relationships, please provide a database dump file (ZIP file) of your website in below private detail box, I need to test and debug it in my localhost, thanks

#622406

Thanks for the details, I am downloading the file, will feedback if there is anything found

#622462

Here are what I found, in the new many-to-many relationship of Types plugin 2.3 b3 + Views plugin 2.6 b3,
You can not query the Intermediary post type "music-release-music" or edit it directly, so you can not display the custom field "wpcf-purchase-link" with Views plugin, if you agree, we can take it as a feature request, our developers will evaluate it.

Currently there is a workaround,
1) you can setup a normal post type "music-release-music-2", register custom field "wpcf-purchase-link" in it.

2) setup one-to-many relationship between post type "music-release" and "music-release-music-2"
setup one-to-many relationship between post type "music-store" and "music-release-music-2"

Then in the single "music-release" post, you will be able to create a view to list "music-release-music-2" posts
https://toolset.com/documentation/customizing-sites-using-php/displaying-child-posts/
and display it's "wpcf-purchase-link" field value, and the related "music-store" fields.

#622464

Hi Luo,

I am aware that currently the beta of views does not have the functionality to query the intermediate post type at the moment. I have been told that this functionality will be built in by the time of release though, hopefully in the next beta.

Having said that, your reply is worrisome...

I find it hard to believe that this would be a 'feature request ' for the future... why on earth would the intermediary post types even have custom fields if it was impossible to query them, these custom fields would be black holes with no functional purpose?

Am I to believe that the final released version of the new Toolset suite with the new Relationships is not currently planning to allow the intermediary post types to be queried, and their custom fields will not be able to be displayed?

----------------

However my support request is not for the views plugin. I am requesting support for the Post Relationships API.

I am specifically referring and requiring support for the recently published 'Post Relationships API', linked to here:

https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/

You recently had a blog post referring to the new API and asking users to alter their sites to use it, here:

https://toolset.com/2018/03/how-to-prepare-your-sites-for-the-new-post-relationships-coming-in-types-2-3/

On the above linked documentation it specifically details the new API functionality and demonstrates how to access intermediary post types. In the demonstrated example, the code is querying 'books', these would be the intermediary post types belonging to 'writer' and 'publisher' parents - using legacy nonmenclature

In the example, they are being ordered by a custom field of the intermediary, wpcf-genre. Here is the code example provided by the API documentation:

$query = new WP_Query( 
    array(
        'post_type' => 'book',
        'numberposts' => -1,
        'toolset_relationships' => array(
            array(
                'role' => 'child',
                'related_to' => get_the_ID(),
                'relationship' => array( 'writer', 'book' )
            ),
            array(
                'role' => 'parent',
                'related_to' => $selected_post,
                'relationship' => 'new-many-to-many-relationship'
            )
        ),
        'meta_key' => 'wpcf-genre',
        'orderby' => 'meta_value',
        'order' => 'ASC',
    )
);
$posts = $query->posts;

This is quoted exactly from the documentation:

"When new post relationships are enabled, it also becomes possible to query by (or for) intermediary post types of many-to-many relationships."

However no variation of this code using my cpt and relationship slugs is working.

The code in the original ticket allowed me to query the intermediary post type, but it was returning all the intermediary post types, not just the intermediary that belonged in the relationship.

Also the API documentation features these functions:

toolset_get_parent_post_by_type()
toolset_get_related_posts()
toolset_get_related_posts()

However when i try to use these in my single cpt template I am getting 'undefined function' error?

Basically I am trying to do what your blog post suggested and start preparing the site for the new Post Relationships, but the having difficulty with the new API code and functions.

I hope I am being a bit more clear, sorry for any confusion that I may be referring to querying via the views plugin.

Jeff

#622501

There is a misunderstanding, the document you mentioned above:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#wp_query-argument-for-querying-by-related-posts
Which is for feature version of Types plugin version 2.3,

Toolset Types 2.3 release (currently in Beta) introduces a completely rewritten post relationship features.

the functions you mentioned above:

toolset_get_parent_post_by_type()
toolset_get_related_posts()
toolset_get_related_posts()

will work in beta version of Types plugin version 2.3-b3

You can download it here:
https://toolset.com/account/downloads/
Choose channel: Beta

#622505
Screen Shot 2018-03-06 at 6.08.27 PM.png

Haha, yup I know... no confusion

I am running both the beta of types and views. Please see the screenshot.

That is the problem. I am using the beta and those functions are coming up as undefined and the new query is also not working as expected.

I have been testing the beta for weeks now, and have been eagerly anticipating either the inclusion of querying intermediaries via the Views GUI, or being able to use the new Relationships API for about 2 months.

Each time you update the beta I check to see if it has included this capability. Please see my previous three support threads:

1.
https://toolset.com/forums/topic/types-beta-2-3-new-relationships-moved-out-of-post-meta/

2.
https://toolset.com/forums/topic/types-views-beta/

3.
https://toolset.com/forums/topic/views-types-beta-displaying-intermediary-post-fields/

Unfortunatly, in each of these support tickets, the functionality was not yet included.

So, when you released the latest blog post saying the new Relationships API has been released, I jumped straight back in to test it...

(https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#wp_query-argument-for-querying-by-related-posts)

We are on the right track now...

I am running the betas, and have the new relationships enabled, and created new relationships...

But the new query structure is giving me problems.

And the new functions are returning 'undefined'

?

p.s my apologies, I assumed that it was given that I was talking about the beta's since the support ticket is regarding the new Post Relationship API which is introduced with the betas

I should have been much clearer at the beginning.

#622828

Yes, you are right, in Types plugin Beta version 2.3 B3, if you use function:
toolset_get_parent_post_by_type()
toolset_get_related_posts()
toolset_get_related_posts()
You will get the PHP errors:
'call to undefined function' error for each of these new API functions.

I just searched it inside the source codes of Types plugin Beta version 2.3 B3, there isn't any PHP codes for those function:
toolset_get_parent_post_by_type()
toolset_get_related_posts()
toolset_get_related_posts()

But you can use those function with the latest stable version Types 2.2.22
You can download it here:
https://toolset.com/account/downloads/

For example, you can try these:
1) Deactivate the Types beta, and activate the stable Types plugin 2.2.22
2) put below codes into your theme file single.php:

$intermediary_posts = toolset_get_related_posts( 
	get_the_ID(), //Post to query by.
	'music-release-music-store',  //Slug of the relationship to query by 
	'parent', //Name of the element role to query by.
	100, //Maximum number of returned results
	0, //Result offset
	array(),//Additional query arguments
	'post_id', //Determines return type
	'intermediary' // which posts from the relationship should be returned
);
foreach($intermediary_posts as $post_id){
	echo $post_id;
	echo get_post_meta($post_id, 'wpcf-purchase-link', true);
	
}

More help:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

3) Test the single "music-release" post in front-end, for example post ID 79:
enlace oculto

You will get the related "intermediary" post ID 196, and the field value of "wpcf-purchase-link":
enlace oculto

But there isn't new many-to-many relationship feature within the Types plugin 2.2.22, this new many-to-many relationship feature and the document only work for the future stable version of Types plugin 2.3.
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#wp_query-argument-for-querying-by-related-posts

#622830

Hey Luo,

Great, thanks for clearing that up... I thought I was going crazy.

Basically that documentation was for people using the new stable, to prepare for the upcoming release featuring the Relationships API, but that Relationships API code hasn't been included for testing in the new beta.

This would suggest that the stable types currently includes the new relationships?

I will have a look at this later, and come back with any questions if I have them, otherwise - if no questions, I'll close the ticket.

Thanks again for your help

Jeff

#622842

For the new question
This would suggest that the stable types currently includes the new relationships?

Yes, as you can see, our developers are working on it, but I am not sure when will it be released, I suggest you subscribe to our blog to get the updated news:
https://toolset.com/blog/

#622846

I am subscribed,

That's why i jumped on the mention of the New Relationships API.... been waiting a while for this.

I just assumed that it would also have been included in the beta since the beta's are being used for testing the new Relationships API.

Honestly, a little strange that the beta plugins being used to test the new Relationships, don't include the new Relationships API, while the stable channel does... not confusing at all.

Thanks again for clearing that up for me.

Jeff

#622851

The Types Beta version 2.3-b3 is released before stable version 2.2.22, and there isn't Relationships API within Beta version 2.3-b3, maybe there will be in the feature version of Types Beta version, please update this thread when you still need assistance for it. thanks