0

I have a custom post type named 'lawyer' and it contains various custom fields. two of the custom fields are 'state' and 'city'. i want to create a archive page or simple page(anyone will do) which can list all the values of city for specific state. for example i have 10 listings. 5 of which have custom field 'state' value as "albama" now i want list of the values 'city' of all those five listings which has state as "albama" but i also want that reduntant city doesnt not showup. i.e. if 3 of cities are "florence" then they only showup once.

0

The first thing I would do, if I were you, would be to change the data structure so that the location would a hierarchical custom taxonomy where states were parent terms and cities their child terms.

Then you could create a custom taxonomy template for your location taxonomy. In this template you'd display list of child terms when the queried object is a parent term.


EDIT 3.9.2019

Here is a very basic example how to get the child terms in archive-{post_type}.php template

// WP_Term object
$current_term = get_queried_object();
// maybe array of WP_Term objects
$children = get_terms( array(
  'taxonomy' => $current_term->taxonomy,
  'parent'   => $current_term->term_id
) );
// If any child terms exists
if ( $children && ! is_wp_error( $children ) ) : ?>
  <ul>
    <?php foreach ( $children as $child_term ) : ?>
      <li><?php echo esc_html( $child_term->name ); ?></li>
    <?php endforeach; ?>
  </ul>
<?php endif; 

A good place to learn more about custom post types is the Plugin Handbook, https://developer.wordpress.org/plugins/post-types/working-with-custom-post-types/

And if you're not very familiar with PHP yet, have a look at the PHP documentation Getting Started guide, https://www.php.net/manual/en/getting-started.php, or use your preferred search engine to find tutorials and beginner level courses.

  • hey thanks for reply but can you send code forr template as i dont have much experience with php – Chinmay Ramraika Aug 28 at 11:46
  • @ChinmayRamraika, I added a simple example to my answer, which you can use as a starting point for learning. And if you need help with customizing your site, please consider hiring a professional developer. – Antti Koskinen Sep 3 at 9:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.