Skip Navigation

[Resolved] wp query not working

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)

This topic contains 3 replies, has 2 voices.

Last updated by samE-6 1 year, 6 months ago.

Assisted by: Waqar.

Author
Posts
#2621537

Hi!
I am using wp query and meta query according to your documentation but it is not working.

Here is my wp query. Although the value 'senior' is assigned to only one profile post but it displaying every profile post.

$args = array(
'post_type' => 'profile',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'wpcf-level',
'value' => 'senior',
'compare' => '='
)
)
);

$query = new WP_Query($args);

if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
// Display the profile information as desired

echo '<h2>' . get_the_title() . '</h2>';
echo '<h6>' . types_render_field('level', array('output' => 'raw')) . '</h6>';
echo '<h6>' . types_render_field('job_title', array('output' => 'raw')) . '</h6>';
$postmetas = get_post_meta(get_the_ID());

foreach($postmetas as $meta_key=>$meta_value) {
echo $meta_key . ' : ' . $meta_value[0] . '<br/>';
}
}
wp_reset_postdata();
} else {
// No profiles found
echo '<h6>No profiles found.</h6>';
}

#2621541

Hi,

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

Since you are filtering the query only using a single custom field, you can structure your '$args' array like this:
( ref: https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters )


$args = array(
	'post_type' => 'profile',
	'posts_per_page' => -1,
	'meta_key' => 'wpcf-level',
	'meta_value' => 'senior'
);

Please test this change and let me know how it goes.

regards,
Waqar

#2621637

Hi! It works for only one custom field but I have several custom fields to be filtered. But it doesn't work when I use meta query as an array for multiple fields. I also tried to 'compare' as 'LIKE' instead of '=' but didn't work out

Here is my code
$args = array(
'post_type' => 'profile',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'wpcf-job_title',
'value' => 'engineer',
'compare' => '='
),
array(
'key' => 'wpcf-level',
'value' => 'senior',
'compare' => '='
),
array(
'key' => 'wpcf-location',
'value' => 'us',
'compare' => '='
)
)
);

$query = new WP_Query($args);

if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
// Display the profile information as desired

echo '<h2>' . get_the_title() . '</h2>';
echo '<h6>' . types_render_field('level', array('output' => 'raw')) . '</h6>';
echo '<h6>' . types_render_field('job_title', array('output' => 'raw')) . '</h6>';
$postmetas = get_post_meta(get_the_ID());
}
wp_reset_postdata();
} else {
// No profiles found
echo '<h6>No profiles found.</h6>';
}

#2621815

My issue is resolved now. Thank you!