Skip Navigation

[Resolved] Help with meta query with toolset

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like some information about using WP_Query with custom fields. I would also like to know how to get custom field information from a related post.

Solution: Use the "wpcf-" prefix for any Types custom field slug when working with WP APIs. Use the Toolset Post Relationships API to get related posts, and use the types_render_field API to display a custom field from a post.

Relevant Documentation:

This support ticket is created 5 years, 5 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
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)

Author
Posts
#1302305

Hello,

I need help on 2 subjects:

1 - I am trying to build a custom query with native wordpress fonctions but it doesn't work. Here is my code:

i am getting the values from to query string

if($_GET['searchcentre'] && !empty($_GET['searchcentre']))
{
$searchcentre = $_GET['searchcentre'];

}

For the query i have this:

<?php

$args = array(
'post_type' => 'projet',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'searchcenter',
'value' => $searchcentre,
'compare' => '='
)
)
);

$query = new WP_Query($args);
?>

In the query i have 12 meta query arrays but i just took this to simplify.

It looks correct by doesn't work at all.

2. And for the second subject, i have 2 post-types (Projet and Centre) and they are related with many to many relation.
The "Projet" post-type has a field "centre" with the name of a centre from the "Centre" post-type and i would like to know how can i get the address of the centre in the "Projet" post-type from the "Centre post-type"?

Thank's for your help, i am new to this and my english is not great.

Thank you

#1302497

1 - I am trying to build a custom query with native wordpress fonctions but it doesn't work.
Is "searchcenter" a field created in Toolset Types? If so, then you should use the "wpcf-" prefix. If the slug in wp-admin is "searchcenter", then it will look like this:

'key' => 'wpcf-searchcenter',

The "Projet" post-type has a field "centre" with the name of a centre from the "Centre" post-type and i would like to know how can i get the address of the centre in the "Projet" post-type from the "Centre post-type"?
In a Content Template, you can use the Fields and Views button to insert the centre address field. Then use the "Post Selection" tab to select a related post: https://toolset.com/documentation/post-relationships/how-to-display-related-posts-with-toolset/

In PHP, you must use the Post Relationships API to get the related post ID. Then you can use types_render_field to render the field, or you can use get_post_meta to display the raw data from the database.

To get a related post ID:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

To display a custom field using types_render_field and the "item" attribute:
https://toolset.com/documentation/customizing-sites-using-php/functions/
Click "+More info" beneath any field type to see examples.

#1302773

My issue is resolved now. Thank you!