Skip Navigation

[Resolved] wpv-control-post-relationship / wpv-control-post-ancestor intersect results

This support ticket is created 4 years, 10 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 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 7 replies, has 2 voices.

Last updated by pharmaklaus 4 years, 10 months ago.

Assisted by: Shane.

Author
Posts
#1503217

Hi,
i have a db with two custom posts: ricetta (recipe) e ingrediente (ingredients) in a many to many relationship with one custom field quantità (quantity).

In the "multi-ricetta" view i use a multi select to filter the recipes by ingredients associated.

When i select two ingredients the results shows the sum of the recipes of the first ingredient and the second one.

I want to change this behaviour not to add but intersect the results.
When i select two/three/four/etc. ingredients i wish the recipes that are in common with the two/three/four/etc. ingredients.

hidden link

#1503537

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Screenshot 2020-02-11 at 9.57.30 AM.png

Hello,

Thank you for getting in touch.

If I understand this correctly you want to filter foods that contain lets say "Acqua AND Alette di pollo" not a logic that say "Acqua OR Alette di pollo"

What you need to do is edit your field relationship in the Query filter section of your view and set it to an AND logic, See Screenshot.

Thanks,
Shane

#1503923
Edit View ‹ foodb — WordPress.png

Hello Shane,
If I understand this correctly you want to filter foods that contain lets say "Acqua AND Alette di pollo" not a logic that say "Acqua OR Alette di pollo"

Yes

I'm using the "Filter by post relationship or repeatable fields group owner:
Select posts in a Ricette Ingredienti relationship that are related to the Post with ID set by the URL parameter wpv-relationship-filter.
eg. hidden link"

I don't know where is the AND/OR select you shown me

If you need i can share a login so you can check directly.
Cheers

#1504043

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hello,

Thank you for the screenshot, unfortunately in this case you won't be able to use an AND logic with the relationship filter as it doesn't provide for this.

This option is available for custom fields with multiple select values as well as taxonomies but not for Post relationship ancestor filters.

Thanks,
Shane

#1505353

Hello
I'm thinking about how to solve the problem, maybe you could use a condition based on a shortcode.

Eg Select 3 ingredients, it shows the only recipe that has those three ingredients. If they are not all present, the result is not shown.

1) create a query of ingredients filtered with "Filter by post relationship or repeatable fields group owner" -> wpv-relationship-filter
2) insert a condition in the loop where it should evaluate whether the current recipe has all the elements associated with it, when it finds the recipe with all three ingredients then it is true

The shortcode could take the parameters via $ _GET ['wpv-relationship-filter'] in order to have the id of the associated ingredients, then it would be necessary to test the recipe if it has relations with the associated ids.

I don't know if it's a good idea and I don't know how to implement it, I saw some questions on the forum but I didn't find something that is right for me. If it is feasible, can you give me some advice on how to proceed?

Thank you

#1505599

I think I solved it by creating a shortcode and evaluating the connections through id.

A starting point was this answer:
https://toolset.com/forums/topic/conditional-output-based-on-post-relationships/

view

[wpv-layout-start]
    [wpv-items-found]
      <!-- wpv-loop-start -->
	<div class="container-fluid wpv-loop js-wpv-loop my-5">
      <div class="row ">
	<wpv-loop wrap="4" pad="true">
		[wpv-item index=1]
          [wpv-conditional if="( '[getparam]' eq '1' )"]
          <div class="col-md-3 mb-3">
            [wpv-post-body view_template="loop-item-in-ricette-isotope"]
          </div>
          [/wpv-conditional]
		[wpv-item index=2]
          [wpv-conditional if="( '[getparam]' eq '1' )"]
          <div class="col-md-3 mb-3">
            [wpv-post-body view_template="loop-item-in-ricette-isotope"]
          </div>
          [/wpv-conditional]
		[wpv-item index=3]
          [wpv-conditional if="( '[getparam]' eq '1' )"]
          <div class="col-md-3 mb-3">
            [wpv-post-body view_template="loop-item-in-ricette-isotope"]
          </div>
          [/wpv-conditional]
		[wpv-item index=4]
          [wpv-conditional if="( '[getparam]' eq '1' )"]
          <div class="col-md-3 mb-3">
            [wpv-post-body view_template="loop-item-in-ricette-isotope"]
          </div>
          [/wpv-conditional]
		[wpv-item index=pad]
			<div class="col-md-3"></div>
		[wpv-item index=pad-last]
			<div class="col-md-3"></div>
	</wpv-loop>
		</div>
	</div>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-pager-nav-links output="bootstrap"]
	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]

shortcode

add_shortcode( 'getparam', function(){
	// I added this to the top of page.php before the get_header() call
	$urlparam = $_GET['wpv-relationship-filter'];

	if ( $urlparam ) {

    $atts = shortcode_atts(
        array(
            'relationship'      =>   'ricetta-ingrediente',
        ),
        $atts
    );

    global $post;
    $count = 0;

    $relationship = toolset_get_relationship( $atts['relationship'] );

    if ( $relationship ) {

        $parent = $relationship['roles']['parent']['types'][0];
        $child = $relationship['roles']['child']['types'][0];
        $type = $post->post_type;

        $origin = ( $parent == $type ) ? 'parent' : 'child';

        // Get connected posts
        $connections = toolset_get_related_posts( $post->ID, $atts['relationship'], $origin, 9999, 0, array(), 'post_id', 'other', null, 'ASC', true, $count );
				$valueIntersect = array_intersect($urlparam, $connections);
				$containsAllValues = array_diff($urlparam, $valueIntersect);

				if (count($containsAllValues)==0){
				  return true;
				}else{
				  return false;
				}
    }
	}
});
#1505843

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hello,

Happy to see that you were able to resolve the issue.

However given that this is a custom solution, we here wouldn't be able to debug and resolve the problem if an issue arises.

We can however have a look to see what the issue is and make a recommendation .

Thanks,
Shane

#1505845

My issue is resolved now. Thank you!