Support » Plugin: Pods - Custom Content Types and Fields » Where clause in relationship field

  • Resolved littleweewebsites

    (@littleweewebsites)


    I am trying to add a where clause in a relationship field.

    I have a relationship field in my project pod, related to a field in my video pod. The relationship is 2-way, so I want to only show videos in the list that are related to the specific project.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Jim True

    (@jimtrue)

    You don’t actually relate specific fields to specific fields. The relationships are to the entire ‘post type’ on the other side of the relationship and what we store is the post ID within the relationship field that matches the select posts on the other side of the relationship.

    If you setup the relationship correctly between projects > videos and from videos > projects (bidirectional field selected in each relationship field to communicate with the relationship field on the other side), the relationship will only show videos that were added to the specific project in the list view and from the video, it will only show the project that was selected for the particular video.

    Customized WHERE (if that’s what you’re trying to do) is used primarily when there is _another_ field, usually something related to the logged-in user that you want to use to filter the results in the relationship.

    I hope that helps. If not, please give me additional specifics.

    Thread Starter littleweewebsites

    (@littleweewebsites)

    Hi Jim,

    So i have the relationship set up, but all of the videos are displaying in the project, even if they are not selected. It shows all videos, with the unrelated ones unchecked.

    I have multi select from project to video, and single select video to project.

    Hi Jim, any resource online on how to use the WHERE clause?
    Imagine I have an object (pod), called cat, it has a name and gender, mother, father and descendents.

    I have 3 fields as relationships, mother, father, and descendents.

    • mother is a simple relationship to the same cat pod, but WHERE gender = female
    • father is a simple relationship to the same cat pod, but WHERE gender = male
    • descendents is a multiple relationship to the same cat pod, but WHERE not in mother and father ids

    How can I achieve this?

    I was able to achieve half the requirements by adding on the where clause this:
    gender.meta_value = ‘f’and for the father, gender.meta_value = ‘m’

    I forgot to mention that descendents is a bi-direccional relationship to cat, linked to the father and mother fields.

    In the form list, I would like to remove the entry of the item we are editing from the father/mother list.
    ex. If I’m editing the cat pod named “garfield” and since he’s male, how can I remove it from the father list?

    • This reply was modified 4 years, 1 month ago by brunomac. Reason: incomplete question
    Plugin Author Jim True

    (@jimtrue)

    @brunomac You’ve jumped onto an existing forum post where the first person is still having issues and pretty much switched the topic to your question without adding to or answering the original poster’s question. That’s not helpful for @littleweewebsites who was trying to get their original question answered. Please create your own forum question to get assistance.

    @littleweewebsites How are you going about ‘displaying’ the relationships in your project? Now it’s much clearer that the problem wasn’t in the ‘selection’ or the back-end but in the front-end display after making the relationship connections.

    Display depends on ‘context’. If you’re on a single post for the videos or a single post for the project, you want to, within that template for display, loop through the relationship field to display the related videos or related project.

    If you’re using WP Theme Templates in PHP, you want to use this method to get related values from a relationship field: https://pods.io/tutorials/get-values-from-a-custom-relationship-field/

    If you’re using a Pods Template and the magic tag method, if your field is related_videos and you’re in the Projects single post type, you’d simply use the [each] template tag:

    
    [each related_videos]
    <li><a href="{@permalink,esc_url}">{@post_title}</a></li>
    [/each]
    

    This is described in Template Tags in Pods Templates: https://pods.io/docs/build/template-tags-in-pods-templates/ and also demonstrated in this video: https://youtu.be/fxpH68u3sgw (at around the 21 minute mark)

    ok, will do.
    sorry Jim

    Plugin Author Jim True

    (@jimtrue)

    NO worries, just keeps these forums from getting confusing.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Where clause in relationship field’ is closed to new replies.