[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 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.
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.
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.
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.
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:
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.