Skip Navigation

[Resolved] Unable to list Child Posts Using Toolset API and Custom Code Section

This support ticket is created 3 years, 10 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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 26 replies, has 3 voices.

Last updated by himanshuS 3 years, 10 months ago.

Assisted by: Shane.

Author
Posts
#1890803

I have two post types: 1)endorsement request and, 2) Endorsement.
They are in a one to one relationship. I am using elementor to build my templates and want to use custom query to list all the endorsement received by the current user. I am using Toolset relationship API function to get the result but the query does not work.
I have written similar code earlier without the current user scenario and it all works. The $endorsement_ids_array returns value : Array ( [0] => 5958 [1] => 8370 [2] => 9819 [3] => 9824 [4] => 9831 ) but nothing happens on the set query function.

What am I missing? Help appreciated.

Custom code:
<?php
/**
* New custom code snippet (replace this with snippet description).
*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.
add_action( 'endorsement-received-by-current-user', function( $query ) {
global $wpdb;
global $post;
$user_id = get_current_user_id();
$args = array(
'author' => $user_id,
'orderby' => 'post_date',
'order' => 'ASC',
'post_type' => 'endorsement-request',
'post_status' => 'publish'
);
$the_query = new WP_Query( $args );
$endorsement_ids_array = array();
//$endorsement_requests_array = array();
// The Loop
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
// Do Stuff
$post_id = get_the_id();
$endorsement_id = toolset_get_related_post($post_id, 'endorsement-request-to-endorsement', 'child', 'publish');
If ($endorsement_id!=0)
{
$endorsement_ids_array[ ]= $endorsement_id;
}
endwhile;
endif;
$query->set('post__in', $endorsement_ids_array);
// Reset Post Data
wp_reset_postdata();
});
?>

#1891381

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

I see you are using add_action() but at what action this code should be fired?

This is a custom code and to entertain the custom code is beyond the scope of our support policy, but as you suppose to send me access details to your site with other tickets you created, I will look at once.

Can you please tell me when this code will be fired and what is your expected output?
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#1892049

Minesh
Supporter

Languages: English (English )

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

I just added the print_r() to see if the hook you added that belongs to elementor is called or not.

As you can see below:

add_action('elementor/query/endorsement-received-by-current-user', function( $query ) {
   
   echo "<pre>";
   print_r("hi there");
   exit;

But when I refresh the page: hidden link

I do not see the action is call and i'm not sure why you are asking the help for the custom code hook that belongs to Elementor. You should contact the elementor support and at lease the hook you added should be called.

#1896807

Minesh,

I have checked with elementor and only this code fails and other simple code works such as this one:

add_action( 'elementor/query/draft-posts', function( $query ) {
// Here we set the query to fetch posts with
// post status 'publish' and 'draft'.
// Refer to WP_Query documentation in WP codex for values list.
$query->set( 'post_status', [ 'publish', 'draft'] );
} );

Meanwhile, while searching I found this thread that aligns exactly with my scenario. I need elementor because it is better than and more intuitive but love the power to toolset. That's why I want to use both of them to get a list of child posts. The code I shared earlier does not work but may be you can help me do the setup as per this note on toolset:
https://toolset.com/forums/topic/query-related-posts-with-elementor-query-id-function/

Solution suggested by the post: "After all those problems I tried to figure out a different solution and switched back to the view solution with the newly released Toolset View Widget for Elementor.
So I created a dynamic Elementor template for the loop view and pasted the shortcode into a Toolset view. I used the Toolset view functionality to filter the relationships and then added the view to my Elementor page template with the Toolset view widget."

My ask: I am struggling to configure it. In fact, I have a shortcode ready that has all post-ids I want to show but I am not sure how to use a shortcode (not a views shortcode) to create a view that shows posts with that id.

I really need to show child posts in elementor and was wondering if you can help me figure out a workaround based on the info I shared.

#1896833

Minesh
Supporter

Languages: English (English )

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

I see with your code you are using the function toolset_get_related_post() to get the related posts.

But instead you should replace the toolset_get_related_post() with WP_Query() as shown with the following example to fetch the related posts:
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/#wp_query-argument-for-querying-by-related-posts

I hope that should work and that is what the solution shared with the reference ticket you shared.

#1896895

Thanks for sharing that. I used your logic and it it doesn't seem to work. Can you please have a look and let me know what am I missing?

Code:

add_action( 'elementor/query/test-query-test', function( $query ) {

$user_id = get_current_user_id();
$args = array(
'author' => $user_id,
'post_type' => 'endorsement-request',
'post_status' => 'publish'
);
$the_query = new WP_Query( $args);
$endorsement_requests_array = array();

if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
// Do Stuff
$endorsement_request_post_id = get_the_id();
$query_endorsement = new WP_Query(
array(
'post_type' => 'endorsement',
'posts_per_page' => -1,
//new toolset_relationships query argument
'toolset_relationships' => array(
'role' => 'child',
'related_to' => get_the_ID(),
// this will work only with relationships that have existed before the migration
// if possible, use the relationship slug instead of an array
'relationship' => 'endorsement-request-to-endorsement')
)
);
// $endorsement_id = toolset_get_related_post($endorsement_request_post_id, 'endorsement-request-to-endorsement', 'child', 'publish');
$endorsement = $query_endorsement->posts;
$endorsement_id = $endorsement->ID;
if ($endorsement_id!=0){
$endorsement_ids_array[]= $endorsement_id;
}
endwhile;
endif;
$query->set( 'post__in', $endorsement_ids_array );
wp_reset_postdata();
} );

#1896909

Minesh
Supporter

Languages: English (English )

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

Ok - but as said, you should not use any of the post relationship functions. You should also try to replace the toolset_get_related_post() function with WP_Query() and make sure it return the ID or you should grab the ID from the return result.

If it still does not work, you will have to debug your code line by line by adding breaks and find out where it's broken.

#1897375

Minesh,

I don't understand. Why is the wp_query methond preferred over the relationship API function when you have clear documentation and guidance on using the API - https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts.

In fact, it is recommended to use these functions:
Besides the new WP_Query argument, there are more functions that can be safely used for getting related posts. Their advantage is that they were designed for post relationships and in certain situations may be lighter and easier to use than WP_Query. You can find the full list on the main Toolset Relationships API page.

I need to really solve this part and would like to know the right protocol to get the related posts in toolset.

#1897415

Minesh
Supporter

Languages: English (English )

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

Lets forget everything first, can you first make sure that you able to trigger the elementor code as I suggested here:
=> https://toolset.com/forums/topic/unable-to-list-child-posts-using-toolset-api-and-custom-code-section/#post-1892049

Do you see the elementor hook triggered? If the hook is not triggered how come any code you added within that hook is going to trigger. Can you show me you able to trigger the Elementor hook.

#1897467
elementor1.png

Hook triggers because another code written in toolset custom code section to select draft and published posts and it works on another page.

The code that works:
add_action( 'elementor/query/draft-posts', function( $query ) {
// Here we set the query to fetch posts with
// post status 'publish' and 'draft'.
// Refer to WP_Query documentation in WP codex for values list.
$query->set( 'post_status', [ 'publish', 'draft'] );
} );

The page on on which it works: hidden link

#1897477
published and draft posts.png

Image of published and draft posts.

#1897489

Minesh
Supporter

Languages: English (English )

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

I'm not talking about the page where it works but where the page it does not work.

Your target page is this one: hidden link

And as shared with my previous reply, can you show me the hook triggers on your above target page.
https://toolset.com/forums/topic/unable-to-list-child-posts-using-toolset-api-and-custom-code-section/#post-1892049

If no, you should contact Elementor as we are Toolset support, not Elementor and honestly, I do not have any experience with elementor hooks.

In addition to that, it seems we are in completely opposite timezones, if you want I can pass this ticket to supporter who works in this timezone.

#1897503
one post.png

I just checked and it does work on the page.

Here is the code to just get one post:
add_action( 'elementor/query/show-single-post', function( $query ) {
// Here we set the query to fetch posts with
// post status 'publish' and 'draft'.
// Refer to WP_Query documentation in WP codex for values list.
$query->set( 'p', 10134 );
} );

Image attached. So it is not an elementor issue.

It would be great if you could pass this case to someone in the US region.

#1897759

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Himanshu,

Minesh passed this ticket onto me.

Taking a look through the thread i'm not 100% sure what exactly you are trying to achieve.

Is this not something that can be built out using our Blocks plugin to generate the queries for you ? From what I see you simply want to list out the child posts based on the current user.

Please provide some clarity here as you should be able to achieve this without using custom code.

Thanks,
Shane

#1897815

Hi Shane,

I have two CPTs - endorsement and endorsement requests.

Registered users can request endorsements and the endorser receives an email to endorse the endorsee. Endorser does not need to be registered.

The endorsement request and endorsement CPTs are in a 1-1 relationship and I want to show all endorsements received by the current user.

I am using elementor to build my site and hence not using Gutenberg or the blocks plugin.

Elementor allows people to add a custom query in the post widget to get the information. I have shared examples of that query above and I have used toolset custom code section to write code for different pages on the site. The elementor custom code functionality works.

However, when I am trying to get list of toolset created child posts for the current user , it just does not work. I have build my entire site using toolset and elementor and want to keep it that way. How can I show all posts (endorsements) that are children of parent (endorsement requests) created by the current user.

In terms of content templates, I have an elementor post template and an elementor loop template and it all works beautifully with Toolset for both endorsement request and endorsement.

I hope this helps.