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>';
}
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
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>';
}
My issue is resolved now. Thank you!