Hi there,
Looking to query a custom post-type by custom fields but want the second meta_query to vary depending on the page slug. Not sure how to go about this whether to duplicate the query or if someone knows something a smarter way to go about this.
e.g. if page slug equals slug1, 2nd meta_query value = value1, if page slug equals slug2, 2nd meta_query value = value2.
`<?php
$args = array(
‘post_type’ => ‘testimonial’,
‘posts_per_page’ => 2,
‘meta_query’ => array(
array(
‘key’ => ‘testimonial_type’,
‘value’ => ‘Video’,
‘compare’ => ‘=’,
),
array(
‘key’ => ‘product’,
‘value’ => ‘———-here—–‘,
‘compare’ => ‘=’,
),
),
);
$testimonial_posts = new WP_Query($args);
?>
<?php if($testimonial_posts->have_posts()) : ?>
…
<?php endif; ?>’
Hi @renee
I believe you were talking about the current page slug where it shows the custom post type, right? In this case, I think you can get the current page slug like this:
global $post;
$page_slug=$post->post_name;
After that, you need to convert the page slug to the value you want like this:
if( $page_slug == "slug1" ){
$value = "value1";
}elseif( $page_slug == "slug2" ){
$value = "value2";
}else{
$value = "value3";
}
Then you can use it like this:
array(
'key' => 'product',
'value' => $value,
'compare' => '=',
),
I hope this makes sense 🙂