Skip Navigation

[Resolved] How to display only posts who do not have related post assigned in many-to-many post relationship

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

Problem:
How to display only posts who do not have related post assigned in many-to-many post relationship

Solution:
You can use the view's filter hook "wpv_filter_query" to change the view's query on fly to get only posts where many-to-many post relationship post does not have child.

You can find the proposed solution in this case with the following reply:
https://toolset.com/forums/topic/views-within-a-view/#post-1784287

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

This support ticket is created 4 years, 2 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 6 replies, has 2 voices.

Last updated by aprilB 4 years, 2 months ago.

Assisted by: Minesh.

Author
Posts
#1780805

Tell us what you are trying to do?

i have a many to many relationship between products and showroom.

I want a page with the following:

Show All products
within the product display each showroom the user created (even if the product is not assigned to the showroom)
if the product is assigned to showroom then display an Edit Link and a delete link.
if the product is not assigned to the showroom then display an Add to Showroom link

is there a way to accomplish this without code? i read the views inside of a view but this only shows me the showrooms that have a relationship with the product.

Is there any documentation that you are following?

Is there a similar example that we can see?

What is the link to your site?

#1781473

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Is this your expected output:

Product 1:
-- showroom 1
-- showroom 2

Product 2:
-- showroom 3
-- showroom 4
-- showroom 5

Product 3:
-- showroom 6
-- showroom 7

if the product is assigned to showroom then display an Edit Link and a delete link.
if the product is not assigned to the showroom then display an Add to Showroom link
==>
I would like to have little more clarification here. You want to display the Edit and Delete link for Product or Showroom?
In addition to that, have you created post form to add/edit post?

#1783485

Hello,

Let me try and clarify. After thinking about how to solve this with toolset i thought it would be easier to first attempt to just add the links to an individual product page. For example, product 1 is in 1 showroom currently and the user has created 3 showrooms. thew product page would show this

Product 1
Currently In:
Showroom 1: Edit showroom connection | Delete from Showroom
Add to Showroom 2
Add to Showroom 3

is an example of a product page on my site.:
hidden link

it uses the content template called "Template for Products 2"

i added an login info to my other ticket so that you can login if you would like.

The hard part os how do you query what show rooms are NOT assigned to a current product. I am not sure how to approach this.

thank u for the help!

#1784287

Minesh
Supporter

Languages: English (English )

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

Can you please check now: hidden link

I've created the following view:
=> hidden link

And added the following filter to "Custom Code" section offered by Toolset:
=> hidden link

add_filter('wpv_filter_query', 'func_filter_post_that_are_not_related', 10, 3);
function func_filter_post_that_are_not_related($query, $view_settings, $view_id) {
  global $post;
  $views = array( 4755 ); // only filter these views
  if( in_array( $view_id, $views ) ) {
    $include_ids = array(); // push all related project IDs here
    $showroom_args = array(
      'post_type' => 'showrooms',
      'posts_per_page' => -1,
    );
    $showrooms = new WP_Query($showroom_args); // get all clients
    foreach ($showrooms->posts as $showroom ) {
       $products = toolset_get_related_posts(
        $showroom->ID,
        'showrooms-product',
        'parent',
        1000000,
        0,
        null,
        'post_id',
        'child'
      );
       
       
      if(empty($products) or !in_array($post->ID,$products)){
      	$include_ids[] = $showroom->ID; // push the related project IDs into this array
      }
      
    } 
    $query['post__in'] = isset( $query['post__in'] ) ? $query['post__in'] : array();
    $query['post__in'] = array_merge($query['post__in'], $include_ids ); // update the main query to exclude related project ids
  }
  return $query;
}

The above filter will help to find out only showrooms where the current product is not connected.

#1784787

Thank you very much. This is great. It took me a bit to process and understand but I think I got it.

I have a few questiona for you now.:

Q1 : Since we know the parent as well that we want to add can you default the parent on the "add-products-to-showrooms-form"? i have read a few tickets that discussed different ways but i have not been able to successfully implement myself.

here is one of the tickets I read and tried to implement: https://toolset.com/forums/topic/set-intermediary-relationships-in-cred-new-post-form/

Q2: If i just wanted to add a connect to the product and showroom (leaving off the price) would it be possible to just call toolset_connect_post after the user clicks a link and reload the page after completion?

Q3: also, i was wondering something..if you look at the loop of the view you created:

[wpv-post-title] - [wpv-post-id] - [cred-relationship-form-link form='add-products-to-showrooms-form' child_item='[wpv-post-id item="$current_page"]' content_template_slug='editing-mode-template-for-showrooms-and-products-relationships'] Add to [wpv-post-title] [/cred-relationship-form-link]

I would like it to say "Add to <SHOWROOM>" but right now it is showing "Add to <Product>" . I am not sure I understand how to reference the showroom name within the cred tag.

#1786059

Minesh
Supporter

Languages: English (English )

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

We entertain only one question per ticket. this will help other users searching on the forum as well as help us to write correct problem resolution summery for the original issue reported.

Q1 : Since we know the parent as well that we want to add can you default the parent on the "add-products-to-showrooms-form"? i have read a few tickets that discussed different ways but i have not been able to successfully implement myself.

here is one of the tickets I read and tried to implement: https://toolset.com/forums/topic/set-intermediary-relationships-in-cred-new-post-form/
==>
Can you please open a new ticket for this.

Q2: If i just wanted to add a connect to the product and showroom (leaving off the price) would it be possible to just call toolset_connect_post after the user clicks a link and reload the page after completion?
==>
No, Toolset post-relationship form do not offer any API hooks yet using which you can call the function toolset_connect_post(). You will require to use the post-relationship form to connect the post.

Q3: also, i was wondering something..if you look at the loop of the view you created:
==>
I've split the ticket for your question #3 and there is issue and I already shared workaround as well as escalated to our next level support.
- https://toolset.com/forums/topic/split-views-within-a-view-cred-relationship-form-link-do-not-display-correct-post-title/

#1786347

My issue is resolved now. Thank you!