Skip Navigation

[Resolved] Trying to query posts in php with a custom field from an intermediary post type

This support ticket is created 4 years, 8 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 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Karachi (GMT+05:00)

Author
Posts
#1577599

Tell us what you are trying to do?

I have a post type called contacts and a post type called contact lists with an intermediary post type between them that contains a Role for the raltionship. what im trying to do is a WP query that returns only the contact post that has the intermediary post type that contains a meta_value set by a form.
the page is here:
hidden link

basically i want to be able to select an option from the form and then return a contact if its role field (contained in the intermediary) contains (LIKE) the value of the option selected.

Is there any documentation that you are following?
havent been able to find an exact document pertaining to my issue

Is there a similar example that we can see?
not that im aware of

What is the link to your site?
hidden link

#1578503

Hi,

Thank you for contacting us and I'd be happy to assist.

If I understand correctly, you'd like to get all the "Contacts Contact lists Intermediary" posts with a specific custom field value for the "Role" field.

And once you have that specific "Contacts Contact lists Intermediary" posts, you can easily reach to their corresponding "Contact" or "Contact lists" posts, using the "item" attribute:
https://toolset.com/documentation/user-guides/views/views-shortcodes/item-attribute/

First, you'll create a post view set to show "Contacts Contact lists Intermediary" posts, with custom field query filter, to match the role field value with a value passed through a shortcode attribute.

Example screenshot: hidden link

In the "Loop Wizard" in the "Loop Editor" section, select the "List with separators" format to get the minimal output from the view which can be used easily in PHP code later.

Example screenshot: hidden link

On the next screen to select the fields to output, choose only the "Post ID".

The "[wpv-post-id]" shortcode will return the post ID of the current intermediary post, but as we need the ID of the corresponding "Contacts" post, you'll replace both instances of this shortcode with [wpv-post-id item="@contact-contact-list.parent"].

Note: This assumes that the relationship slug is "contact-contact-list" and the "Contacts" post is the parent in the relationship. But please update this to match what is on your website.

Example screenshot: hidden link

After that, you can use view's shortcode like this, with the required custom field value, passed as a shortcode attribute:


[wpv-view name="view-slug" rolevalue="required-role-value"]

Note: You'll replace "view-slug" with the actual slug of this view and "required-role-value" with the actual custom field value, that you need to query for.

Once this view is returning the required post IDs correctly, you can either use "render_view" function ( ref: https://toolset.com/documentation/programmer-reference/views-api/#render_view ) or the "do_shortcode" function ( ref: https://developer.wordpress.org/reference/functions/do_shortcode/ ), to use that output in a custom PHP code.

I hope this helps and please let me know if any step is not clear.

regards,
Waqar

#1578931

more looking for the php equivalent

#1581489

My issue is resolved now. Thank you!