Skip Navigation

[Resuelto] exclude intermediary child CPT when filtering relationship

This support ticket is created hace 6 años, 9 meses. 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.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Este tema contiene 11 respuestas, tiene 2 mensajes.

Última actualización por ScottM9386 hace 6 años, 9 meses.

Asistido por: Christian Cox.

Autor
Mensajes
#615412

Hi,

Great plugins!
So I'm trying to build a search. All the basic functions I have down. Here's my problem.

I have 2 CPT's with many-many relationship.
the intermediary child is to remain hidden to the user (eventually my project will be a multi-site, each user will have a subsite. each user will have certain capabilities/role in sub-site)

The intermediary CPT has fields - one being "Exclusive" yes/no

CPT 1 = Songs
CPT 2 = Clients (production houses etc.)
intermediary CPT = song client relationship (remains hidden) has yes/no field called "Exclusive"

I'm trying to create a search:
"Search for all songs that are placed with Client XYZ that have "yes" in Exclusive field"

Really don't know how to do this. Is there documentation?
My first feeble attempt ended up showing the titles of the intermediary post - even though in that CPT I have switched off "publicly queryable"

Thank you!!

#615430

You can accomplish something like this by creating a View of the intermediary CPT, filtered by post relationship (Client > Intermediary CPT), and also filtered by the Exclusive field. Then in the Loop Output editor of that View, you can output information about the parent Song post using shortcodes and the "id" operator. You can specify a parent post type in the "id" operator to set the shortcode context. For example, if your Song CPT slug is "song", then you can show a link to the Song parent post inside a View of the intermediary CPT like this:

...
<wpv-loop>
[wpv-post-link id="$song"]
</wpv-loop>
...

Similarly, you can display custom field values from the Song parent post using the Types field shortcode and id operator:

[types field="some-field" id="$song"][/types]

One limitation to this approach is that it's possible you will see duplicate Song titles in the list of results if a song can be associated with a client using multiple intermediary posts.

More information about displaying information from parent posts here:
https://toolset.com/documentation/user-guides/displaying-fields-of-parent-pages/

#615455

Hi Christian,

Thank you. So, when the search/filter page first loads is when I see the duplicates. So far, in initial testing, it seems to be working when making selections in filter dropdown. Anyway of avoiding these duplicates when page first loads?
I guess one way is to have search results on another page? though this is not ideal perhaps.

Thanks!
Scott.

#615459

I could provide some custom code that will hide all results until at least one filter is chosen, but there is no way to prevent duplicates in the results. If you would like to try that custom code, first make sure that your View is set up to update the page URL as filters are added (either with or without AJAX). Then, select something in each filter and copy + paste the resulting URL here for me to review.

Any other solution will require custom code that falls outside the scope of the support we provide here in the forums. Let me know how you would like to proceed.

#615462

Thanks Christian,

Yes, I'll try the code but I'll have to do it at a later date when I have things more completely set up.
I'll open another ticket at that time and request you to help.
Alternatively, maybe you could send me an example of the code and I could try to figure it out?

Thanks!

#615680

Here is a code sample:

add_filter( 'wpv_filter_query_post_process', 'drop_empty_search_query_post_process', 10, 3 );
function drop_empty_search_query_post_process( $query, $view_settings, $view_id ) {
    $ids = array(123,456);
    if (in_array($view_id, $ids)){
      if (
        // taxonomy filter needs to check for not '0' as well as not empty
        ( isset($_GET['wpv-category']) && $_GET['wpv-category'] != '0' )
          ||
        // location filter needs to check for not '0' as well as not empty
        ( isset($_GET['wpv-location']) && $_GET['wpv-location'] != '0' )
      ) {
      } else {
        $query->posts = array();
        $query->found_posts = 0;
        $query->post_count = 0;
      }
    }
    return $query;
}

Replace 123,456 with a comma-separated list of View IDs where you want to apply this filter. Then modify wpv-category and wpv-location to match your URL parameters. If you have more than two parameters, copy and paste an entire block for each URL parameter like this:

          ||
        // param filter needs to check for not '0' as well as not empty
        ( isset($_GET['wpv-param']) && $_GET['wpv-param'] != '0' )

More information about this filter here:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query_post_process

#615686

Thank you for the code Christian.
So, this code does not prevent duplicates in results, correct?
In the next version that's in BETA will duplicates in results still be an issue?
It's unfortunate as it makes displaying views of M2M related posts very odd.

Thank you for all your help!
Best,
Scott.

#615735
#615745

Perhaps! I was not aware of this workaround.

#615748

what about in the new version coming?
In a post by Nigel from 2016 he said, concerning this issue, "This (avoiding duplicates) may get easier when work on overhauling post relationships within Toolset is completed" - https://toolset.com/forums/topic/filter-archive-by-child-relationship/
Do you know if this is so?
It's a real problem, don't you think? I mean, it's great to have the ability to have relationships, but if you can only show them on a single page of a CPT, that's a problem, for my project anyway.

Thanks for all your great help.

#615757

Yes, I agree this is a problem and the new M2M features will address this in greater detail with more robust filtering options. This particular feature of M2M is not yet in the plugin beta versions, but we hope to have it in beta soon. Data capture and management was scheduled before data visualization.

#615762

GREAT!!!
I hope it will come out sometime this year...
Thanks so much!