• Resolved cleforweb

    (@cleforweb)


    Hello,
    thank you for this fantastic plugin.

    I have custom post type Houses and custom post type Accommodations.
    Each house has several accommodations (apartments) so I created a relationship custom field “Apartments” for the custom post type Houses and associate several accommodations per each house.

    On the Accommodation post I want to display a list of all other accommodations associated for the same house. I want to exclude current post from the list and I am using shortcode like this:

    [pods name=”house” id=”HOUSE-ID” where=”accommodation.id != ‘ACCOMMODATION-ID'” template=”Apartments Grid”]

    which doesn’t work but displays all accommodations associated with defined HOUSE-ID.

    My template code: https://pastebin.com/ZXpyPZ34

    Thank you for any help.

Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Contributor Jim True

    (@jimtrue)

    Typically, when you’re trying to exclude items, you want to use the actual ID of the post that you don’t want to show. Relationship fields store the connections to the other posts on the other side of the relationship by their post ID and for bidirectional relationships, you’re saving the Post ID on both sides of the connection.

    So in your situation, you want to show related accommodations for the related house to the current accommodation post. Do you have a relationship field from accommodation to houses? if you do, then you can include this in your template for Accommodations.

    So the problem you have in your shortcode is that you’re trying to list the other accommodations that are also related to the ‘house’ from this accommodation, but you’re querying ‘house’; you need to be querying ‘accommodation’:

    [pods name="accommodation" where="related_house.id={@related_house.ID} AND ID NOT IN ({@ID})"]

    This basically looks to make sure whatever value is in the current apartment’s house is passed to the list of accommodations and then uses the current apartment’s ID to make sure that’s not the one in the returned list.

    Thread Starter cleforweb

    (@cleforweb)

    Thank you.

    Understood and tried this but got this error:

    Database Error; SQL: SELECT DISTINCT t.* FROM wp_posts AS t LEFT JOIN wp_icl_translations AS wpml_translations ON wpml_translations.element_id = t.ID AND wpml_translations.element_type = ‘post_hb_accommodation’ AND wpml_translations.language_code = ‘en’ LEFT JOIN wp_icl_languages AS wpml_languages ON wpml_languages.code = wpml_translations.language_code AND wpml_languages.active = 1 WHERE ( ( related_house.id={@573} AND ID NOT IN ({@574}) ) AND ( t.post_type = “hb_accommodation” ) AND ( wpml_languages.code IS NOT NULL ) AND ( t.post_status IN ( “publish” ) ) ) ORDER BY t.menu_order, t.post_title, t.post_date LIMIT 0, 15; Response: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘@573} AND ID NOT IN ({@574}) ) AND ( t.post_type = “hb_accommodation” ) AND ‘ at line 16

    Plugin Contributor Jim True

    (@jimtrue)

    You’re not processing the magic tags in your shortcode which means you either 1) haven’t configured your website for Using Shortcodes in Pods Templates, or 2) it looks like you might be putting TWO sets of {}’s around the Magic Tags. That’s not correct, it’s just one {@ID} not {{@ID}}, since it looks like your id’s are populating.

    My shortcode example also assumes your Related House relationship field is called related_house

    Thread Starter cleforweb

    (@cleforweb)

    Thank you for answers and suggestions.

    I added code to wp-config.php and functions.php as stated on suggested link.

    Still no results (the same error). However when I enter code like this

    [pods name=”accommodation” template=”Ex current” where=”parent_house.id=573″]

    it is working (retrieving all accommodations for this house id). I did not entered second part of “where” statement because I didn’ know how to avoid brackets.

    Also after your first answer I entered double brackets but as you suggested

    AND ID NOT IN ({@ID})

    Anyhow I seems that error occurs when using brackets other than [].

    I double checked wp-config and functions code but still stuck with a same error.

    Thank you.

    Thread Starter cleforweb

    (@cleforweb)

    Just found out that shortcode entered to the post like this
    [pods][pods name="hb_accommodation" template="Ex current" where="parent_house.id={@parent_house.ID}"][/pods]
    is working so wp-config.php and functions.php are ok.
    Still get an error with a second part of where statement AND ID NOT IN ({@ID})
    So I want to exclude (hide) current post from the list.
    Thank you.

    Plugin Contributor Jim True

    (@jimtrue)

    What’s the actual SQL Error you’re getting?

    Also not sure why you’re doing [pods][pods... ][/pods] within the post. You shouldn’t have to double-nest your shortcode. The first one should be fine.

    I’d say it’s AND (ID NOT in ('{@ID}'))

    This might go much easier if you joined us on our Slack Chat so we could more quickly discuss what you’re trying and what’s not working.

    https://pods.io/chat/

    Thread Starter cleforweb

    (@cleforweb)

    Thank you.
    Maybe I will join Slack Chat.

    In the meantime I ended up with [pods][pods name="hb_accommodation" template="Ex current" where="parent_house.id={@parent_house.ID}"][/pods] as that is the only working combination showing all accommodtions per house – tested with all plugins disabled and base theme.

    Without nesting I get Database Error; SQL: SELECT DISTINCTt.* FROMwp_postsAStLEFT JOINwp_podsrelASrel_parent_houseONrel_parent_house.field_id= 652 ANDrel_parent_house.item_id=t.IDLEFT JOINwp_postsASparent_houseONparent_house.ID=rel_parent_house.related_item_idLEFT JOINwp_icl_translationsASwpml_translationsONwpml_translations.element_id=t.IDANDwpml_translations.element_type= 'post_hb_accommodation' ANDwpml_translations.language_code= 'en' LEFT JOINwp_icl_languagesASwpml_languagesONwpml_languages.code=wpml_translations.language_codeANDwpml_languages.active= 1 WHERE ( (parent_house.id={@parent_house.id} ) AND (t.post_type= "hb_accommodation" ) AND (wpml_languages.codeIS NOT NULL ) AND (t.post_statusIN ( "publish" ) ) ) ORDER BYt.menu_order,t.post_title,t.post_dateLIMIT 0, 15; Response: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@parent_house.id} ) AND (t.post_type= "hb_accommodation" ) AND (wpml’ at line 24`

    Also hb_accommodation is not Custom post type as I wrote in the first message but Post type extended.

    Thank you.

    Plugin Contributor Jim True

    (@jimtrue)

    hb_accomodation must be another plugin’s post type, is that correct?

    Thread Starter cleforweb

    (@cleforweb)

    Yes this is correct.

    Plugin Contributor Scott Kingsley Clark

    (@sc0ttkclark)

    I just looked back at this again and realized you are using {@parent_house.ID} but you can’t use field variables in the where clause like this currently.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Exclude Relationship field item’ is closed to new replies.