The code you shared works on my test website.
Without access to the website, it won't be possible to troubleshoot the exact cause. But here are a few recommendations:
1). Print/echo '$get_results' array to confirm if the correct Post IDs are being returned.
2). Make sure the slug 'wpcf-title' for the field is correct and that this is a field attached to the child post type (Speakers).
Note: The 'wpcf-' prefix is only added to the custom fields added through the Toolset Types.
3). Also some value should be saved in the 'wpcf-title' field in the child post type (Speakers) whose values are being returned.
Hi Waqar, I did give you access to the copy of the site with duplicator. Isn't that what you are using on your test site? Can you share a screenshot of what displayed on the front end for you?
1) Yes the correct post IDs are being returned
2) Yes it is the correct custom field as it existed before trying intermediary relationship. I did share my previous working code with you.
3) Value is saved
> I did give you access to the copy of the site with duplicator. Isn't that what you are using on your test site?
- The relationship names used on your website's copy are different and the code example that I shared with you was just to give you an example so that you can adopt it according to your website.
If you'd like me to test the code on your website's copy, please share the exact code you're using, along with the event name (which I suspect in your website's case is the 'Locations' post type) that you're using to test.
I am currently using a local test copy because I do not want to make these edits on the live site.
The page to view from your test copy is "/locations/jackson-ms/"
The following code is placed on the single-locations.php file.
Here is the exact code i am using:
<ul class="officecontact">
<?php
add_shortcode('custom_get_hosts', 'custom_get_hosts_func');
function custom_get_hosts_func() {
global $post;
// get related posts of the current post
$query_by_element = $post->ID; // ID of post to get relationship from
$relationship = 'locations_attorney'; // relationship slug
$query_by_role_name = 'parent'; // $query_by_element is a parent in this relation
$limit = 1000; // defaults
$offset = 0; // defaults
$args = array(
'meta_key' => 'wpcf-is-office-contact',
'meta_value' => '1',
'meta_compare' => '=',
); // custom field query
$return = 'post_object'; // We want Post ID in results
$role_name_to_return = 'all'; // return all relationship elements
$get_results = toolset_get_related_posts(
$query_by_element,
$relationship,
$query_by_role_name,
$limit,
$offset,
$args,
$return,
$role_name_to_return
);
ob_start();
foreach ($get_results as $result_item) {
echo '<li><a href="';
//echo get_permalink($result_item);
echo get_permalink($result_item['child']);
echo '">';
//echo $result_item->post_title;
echo get_the_title($result_item['child']);
echo get_post_meta($result_item['child'], 'wpcf-title');
if( get_post_meta($result_item['child'], 'wpcf-title') ):
echo '<span class="ctitle">(' . get_post_meta($result_item['child'], 'wpcf-title', true) . ')</span>';
endif;
echo '</a></li>';
}
return ob_get_clean();
}
?>
<?php echo do_shortcode( '[custom_get_hosts]' ); ?>
</ul>
Screenshot is attached.
Thank you for sharing this.
In my code example, I suggested to call post IDs:
But the code that you've shared is using calling for the post object:
Change that back to the 'post_id' and it will work.
Ok the custom field is showing now but I'm getting an error as well. See attached.
Line 303 on my local copy is:
echo get_post_meta($result_item['child'], 'wpcf-title');
The warning message is showing because it is trying to echo an array and not a string.
Technically you don't need that line in the code in the first place.
As much as we would like to help, 1-1 code customization and PHP code assistance are beyond the scope of support that we provide over the forum.
For general PHP code-related questions, you can consult the community forums like:
https://stackoverflow.com/questions/tagged/php
Hi Waqar, I realized that I had added that line during testing but I really did not need it. Thanks for catching that. It's working fine now.
I wanted to know, is there a reason why your code example was in a form of a shortcode? Is it required to use shortcode to work with the intermediary posts relationships?
You're welcome and glad it is working now.
> I wanted to know, is there a reason why your code example was in a form of a shortcode?
> Is it required to use shortcode to work with the intermediary posts relationships?
- It is not required for any of those features to work from within a shortcode. One can alternatively have a PHP function and use that too.
I usually prefer shortcodes because they can be used in the PHP code (through the 'do_shortcode' function) as well as in the WordPress editors/builders environments in the admin area.
Ok Thanks. I tried adding in the 'orderby' and 'order' arguments to list the names alphabetical by last name but I am not sure if I'm putting it in the right place according to the example on (https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts).
Here is my updated code with the addition of 'orderby' and 'order':
<?php
add_shortcode('custom_get_hosts', 'custom_get_hosts_func');
function custom_get_hosts_func() {
global $post;
// get related posts of the current post
$query_by_element = $post->ID; // ID of post to get relationship from
$relationship = 'locations_attorney'; // relationship slug
$query_by_role_name = 'parent'; // $query_by_element is a parent in this relation
$limit = 1000; // defaults
$offset = 0; // defaults
$args = array(
'meta_key' => 'wpcf-is-office-contact',
'meta_value' => '1',
'meta_compare' => '=',
); // custom field query
$return = 'post_id'; // We want Post ID in results
$role_name_to_return = 'all'; // return all relationship elements
$orderby = array(
'meta_key' => 'wpcf-last-name',
'orderby' => 'meta_value',
'order' => 'DESC',
);
$order = 'DESC';
$get_results = toolset_get_related_posts(
$query_by_element,
$relationship,
$query_by_role_name,
$limit,
$offset,
$args,
$return,
$role_name_to_return,
$orderby,
$order
);
ob_start();
foreach ($get_results as $result_item) {
echo '<li><a href="';
//echo get_permalink($result_item);
echo get_permalink($result_item['child']);
echo '">';
//echo $result_item->post_title;
echo get_the_title($result_item['child']);
//echo get_post_meta($result_item['child'], 'wpcf-title', true);
if( get_post_meta($result_item['child'], 'wpcf-title') ):
echo '<span class="ctitle"> (' . get_post_meta($result_item['child'], 'wpcf-title', true) . ')</span>';
endif;
echo '</a></li>';
}
return ob_get_clean();
}
?>
<?php echo do_shortcode( '[custom_get_hosts]' ); ?>
This requirement makes it complicated because, you need to filter results based on the custom field from the intermediary post type, where order them based on the custom field from the child post type.
To achieve this, it would be better to keep getting the results through the custom field from the intermediary post type and then sort the results through transversing the array:
add_shortcode('custom_get_hosts', 'custom_get_hosts_func');
function custom_get_hosts_func() {
global $post;
// get related posts of the current post
$query_by_element = $post->ID; // ID of post to get relationship from
$relationship = 'locations_attorney'; // relationship slug
$query_by_role_name = 'parent'; // $query_by_element is a parent in this relation
$limit = 1000; // defaults
$offset = 0; // defaults
$args = array(
'meta_key' => 'wpcf-is-office-contact',
'meta_value' => '1',
'meta_compare' => '=',
); // custom field query
$return = 'post_id'; // We want Post ID in results
$role_name_to_return = 'all'; // return all relationship elements
$get_results = toolset_get_related_posts(
$query_by_element,
$relationship,
$query_by_role_name,
$limit,
$offset,
$args,
$return,
$role_name_to_return
);
ob_start();
$final_results = array();
foreach ($get_results as $result_item) {
$result_item_post_title = get_the_title($result_item['child']);
$result_item_permalink = get_permalink($result_item['child']);
$result_item_title = get_post_meta($result_item['child'], 'wpcf-title', true);
$result_item_last_name = get_post_meta($result_item['child'], 'wpcf-last-name', true);
$final_results[$result_item_last_name]['post_title'] = $result_item_post_title;
$final_results[$result_item_last_name]['permalink'] = $result_item_permalink;
$final_results[$result_item_last_name]['title'] = $result_item_title;
}
krsort($final_results); // order the array descending order
foreach ($final_results as $final_result) {
echo '<li><a href="';
echo $final_result['permalink'];
echo '">';
echo $final_result['post_title'];
if( !empty($final_result['post_title']) ):
echo '<span class="ctitle">(' . $final_result['post_title'] . ')</span>';
endif;
echo '</a></li>';
}
return ob_get_clean();
}
Thanks Waqar, that works. Have a great weekend!