Get a list of meta values and search for posts WordPress

how to link meta_values to make a search for posts with the current meta?

The code above, show in a dropdown all the meta_value of city meta_key and cpt post type

<?php if ( have_posts() ) : ?>
//some code for title
<?php 
$args = array(
    'post_type' => 'cpt',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'meta_key' => 'city',
    );
$dbResult = new WP_Query($args);

    echo '<ul class="menu dropdown-menu">';
            while ( $dbResult->have_posts() ) : $dbResult->the_post();
            $mykey_values = get_post_meta($id, 'city', false);
                foreach ( $mykey_values as $key => $value ) { 
                $by_link = esc_url(add_query_arg(array( 'b' => $value ))); ?>
                <li><a href="<?php echo $by_link;?>">  <?php echo $value ?></a></li>  
            <?php   }
                endwhile;   ?>
        <?php wp_reset_query(); ?>

 <?php
    echo '</ul>'; ?>
</nav>

My problem is how to get a link of the values and search.

So to get a link, I tried

 $by_link = esc_url(add_query_arg(array( 'b' => $value ))); ?>
<li><a href="<?php echo $by_link;?>">  <?php echo $value ?></a></li>  

And to search I made another loop and tried to get b value:

<?php 
$meta_value = $_GET['b'] != '' ? $_GET['b'] :  '';
$args2 = array(
    'post_type' => 'cpt',
    'post_status' => 'publish',
    'posts_per_page' => -1,

    'meta_query'    =>  array(
                array(
                'key' => 'city',
                'value'  =>  $meta_value, 
                'compare' => 'LIKE', 
            ),
    ),
);
$dbResult2 = new WP_Query($args2);
 while ( $dbResult2->have_posts() ) : $dbResult2->the_post(); ?>

                <?php get_template_part( 'content', get_post_type() ); ?>

            <?php endwhile; ?>
                    <?php wp_reset_query(); ?>

Any help???

Read more here: Get a list of meta values and search for posts WordPress

Leave a Reply

Your email address will not be published. Required fields are marked *