I'm working on a template for a custom post type - let's call it "cpt-1" . Within this template for cpt-1 I'm trying to display a view of another custom post type ("cpt-2"). This view should contain two filters:
1. Show any cpt-2 that is in a cpt1-cpt2 relationship to the page it is shown on
OR
2. Show any cpt-2 whose author is the same as the page on which the view is shown on
As it appears all query filters are solely connected via an "AND" connection while I need an "OR" connection. I have already tried the suggested solution and added the following function to my function.php file. However it does not show any effect:
function tssupp_filter_query_or($view_args, $view_settings, $view_id)
{
$view_ids = array( 25173 );
if ( in_array($view_id, $view_ids) )
{
if ( !empty($query_args['meta_query']) )
{
$query_args['meta_query']['relation'] = 'OR';
}
}
Hello. Thank you for contacting the Toolset support.
Author query is not a meta query. Can you please share problem URL where you added your view as well as admin access details.
Let me review your structure and then I will be able to guide you in the right direction.
*** 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.
Yes sure! As mentioned before I want to apply the filters to the last view on the page which is called "View - Template News - Startup Beitrag erwähnt". As you can see the View displays post types of the type "Newsfeed" and is placed in a template that displays the post type "startups". In this view I would like to achieve the following:
- Display all "newsfeed" posts which are in a startup-newsfeed relationship, based on the startup which is currently displayed via the template
- Display all "newsfeed" posts which have the same author as the startup which is currently displayed via the template.
So I put the following filters:
1. Relationship-Filter: Posts which are in a Newsfeed-Startup Relationship that are related to the post on which this view is shown
2. Author-Filter: Select Posts which have the same author as the page on which this view is shown.
I've noticed individually the filters work as intended, however as soon as you add a second filter they seem to be connected via an AND query which results in no results being shown in the view unless both filter queries come back as true. Now I would like to change the query to OR so the results would only have to fit one of the filter conditions to display a result.
I hope the idea of what I want to achieve became clearer now.
Can you please share with what frontend user I should check and test the result and for what post?
Actually - its contrary when you add both filters because you apply OR. For instance - user has created 10 post for which he is the author and out of that 5 is connected to the startup post you currently display and 5 are not and you added filter that you want to display related post that means you only want to display 5 posts which are connected and you also wants another 5 posts for that user is the author but its not connected and you added query filter to show connected posts.
Can you please share frontend user access details and for what startup post you want to display the result you mention?
*** 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.
thank you for your response. I have been working on the solution by myself for a little bit and I have gotten it to work so far. However while this code helps me to display all the posts I want to display it somehow disabled the pagination. Do you have any suggestion in how to fix it?
function modify_query( $query, $view_settings, $view_id ) {
if ( $view_id === 25173 ) {
//Select all the posts with certain relationship
$page_id = get_the_ID();
$relationshipposts = toolset_get_related_posts( $page_id, 'newsfeed-startup', ['query_by_role' => 'child', 'role_to_return' => 'parent']);
//Get current page author
$current_post_author_id = get_post_field( 'post_author', get_the_ID() );
//Select all the posts of current page author
// Construct the custom query to fetch posts by the same author
$author_posts_query = new WP_Query( array(
'post_type' => 'newsfeed',
'author' => $current_post_author_id,
'posts_per_page' => -1, // Retrieve all posts belonging to the author
) );
// Output the queried posts
$author_post_ids = array();
// Check if the query has posts
if ( $author_posts_query->have_posts() ) {
while ( $author_posts_query->have_posts() ) {
$author_posts_query->the_post();
$author_post_ids[] = get_the_ID();
}
wp_reset_postdata(); // Reset post data to restore the original post
}
//Variable that will hold the posts which I want to show
$selected_posts = array_merge($author_post_ids,$relationshipposts);
$query_args = array(
'post_type' => 'newsfeed',
'post__in' => $selected_posts,
'posts_per_page' => 3
);
}
return $query_args;
}
Can you please share specific URL of page/post where I can see the issue as well as what frontend user I have to login with, Can you please share login details for frontend user as well?
I have set the next reply to private which means only you and I have access to it.