Skip Navigation

[Resolved] Work with the result from two merged views.

This thread is resolved. Here is a description of the problem and solution.

Problem:
The customer asked how to include checks and filters to see whether some different types of post-relationships and custom taxonomies are set with a post or not.

Solution:
Guided to use select type (yes/no) custom field for each of the post-relationship and custom taxonomy to track the assignment and keep their values up-to-date, whenever a post is created/edited.

Relevant Documentation:
n/a

This support ticket is created 2 years, 7 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 – 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/Karachi (GMT+05:00)

This topic contains 4 replies, has 2 voices.

Last updated by trulsD 2 years, 7 months ago.

Assisted by: Waqar.

Author
Posts
#2369679

I have worked out how to merge two views:
add_filter('wpv_filter_query', 'my_merge_posts', 10, 2);
function my_merge_posts($query_args, $view_id)
{
if ($view_id == 4000) {
$arr1 = get_view_query_results(3000);
$arr2 = get_view_query_results(2000);
$arr3 = array_merge($arr1, $arr2);
$arr = array(0);
foreach ($arr3 as $post) {
$arr[] = $post->ID;
}
$query_args['post__in'] = $arr;
}
return $query_args;
}

I am trying to check if any posts are not in a specific relationship and return how many there are.

I have tried this function:

function ais_check_if_relation_is_NOT_set($postid, $relationslug)
{
$query_by_element = $postid; // ID of post to get relationship connections from
$relationship = $relationslug; // relationship slug
$query_by_role_name = 'child'; // $query_by_element is a parent in this relation
$limit = 1; // limit set to one as we just need to check if related post exists or not
$offset = 0; // defaults
$args = array(); //nothing needed
$return = 'post_id'; // We want Post ID
$role_name_to_return = 'parent'; // We want children.

$foreldreid = toolset_get_related_posts($query_by_element, $relationship, $query_by_role_name, $limit, $offset, $args, $return, $role_name_to_return);
if (empty($foreldreid[0])) {
$answer= 'Has no relationship';
return $answer;
}
}

How do I connect these two functions?
And can I use the first function as a shortcode?

Truls

#2369885

Hi Truls,

Thank you for contacting us and I'd be happy to assist.

If you'd like to include only the posts in the view, which do not have a related parent post, you can use the "toolset_get_related_post" function and won't need a custom function:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post

For example:


add_filter('wpv_filter_query', 'my_merge_posts', 10, 2);
function my_merge_posts($query_args, $view_id)
{
	if ($view_id == 4000) {

		$relationship_slug = 'relationship-slug';

		$arr1 = get_view_query_results(3000);
		$arr2 = get_view_query_results(2000);
		$arr3 = array_merge($arr1, $arr2);
		$arr = array(0);
		foreach ($arr3 as $post) {
			// check if current post has no related parent 
			if ( toolset_get_related_post($post->ID, $relationship_slug) == 0) {
				$arr[] = $post->ID;
			}
		}
		$query_args['post__in'] = $arr;
	}
	return $query_args;
}

In the above code, the array "$arr" will only include those posts, where the related parent post is equal to '0', i.e. no parent post exists for the relationship with the slug "relationship-slug".

I couldn't fully understand how and where you need to use the first function as a shortcode. The custom function attached to the "wpv_filter_query" hook is used to modify the query of the view and doesn't actually output the view's results, on its own.

If you could share some more details about this requirement, I'll be in a better position to guide you accordingly.

regards,
Waqar

#2370115

Hi Waqar, and thanks for the response.

What I'm trying to do is a little bit more complicated.

I have a post type with four relationships and three categories that need to be set before it can be published.
I'm working on a function that summarizes how many "unregistered" posts there are in each relationship/category.

3 posts are missing relationship 1
2 posts are missing category 2
etc

Can I do this with one view and function? I know how to do it with one view and function per relationship/category, but that seems wasteful and code-heavy.

Regards,
Truls

#2370801

Thank you for sharing these details.

I agree that processing each post every time a page is visited to check which categories and relationship connection exists, will become too complex and resource-intensive.

You can try a different approach and add 7 select type custom fields with two possible values:
- yes
- no

Each field will represent a settlement of 4 relationships and 3 categories:

- Relationship 1
- Relationship 2
- Relationship 3
- Relationship 4
- Category 1
- Category 2
- Category 3

Each time a post is added or updated, you'll need to execute a custom function that checks for the existence/setting of each of these relationship connections and categories and then update the relevant field's value with either 'yes' or 'no'.

This way, the heavier or complex evaluation operations can be performed only at the time of post creation or edit, only for that particular post. And while showing the data on the front-end, you can use the custom field values to make any further decisions, without having to perform the evaluation operation, every time for every post.

I hope this makes sense.

#2371547

My issue is resolved now. Thank you!