admin管理员组

文章数量:1203443

I have a custom post type 'locations', with custom fields 'State', 'City', 'Address', and 'Phone'.

I would like to query these fields in an organized list. Let's say I have these three posts:

City: Los Angeles; State: California; Address: 123 Main St.; Phone: 888-111-2222

City: San Jose; State: California; Address: 55 1st St.; Phone: 888-333-4444

City: Brooklyn; State: New York; Address: 9 25th St.; Phone: 888-555-4848

Should display like this:

  • California
    • Los Angeles
      • 123 Main St.
      • 888-111-2222
    • San Jose
      • 55 1st St.
      • 888-333-4444
  • New York
    • Brooklyn
      • 9 25th St.
      • 888-555-4848

So far, I am using this code to grab the states in a list and it is working:

<ul>
<?php
global $wpdb;
$query = "SELECT meta_value, COUNT(post_id) as count FROM $wpdb->postmeta WHERE meta_key = 'state' GROUP BY meta_value ORDER BY meta_value;";
$states = $wpdb->get_results( $query );
foreach( $states as $state ) :
echo'<li>'; echo $state->meta_value; echo '</li>';
endforeach;
?>
</ul>

But this is of course only displaying:

  • California
  • New York

What I'm trying to figure out is how to query the rest of the data I'm looking for.

Thanks in advance!

I have a custom post type 'locations', with custom fields 'State', 'City', 'Address', and 'Phone'.

I would like to query these fields in an organized list. Let's say I have these three posts:

City: Los Angeles; State: California; Address: 123 Main St.; Phone: 888-111-2222

City: San Jose; State: California; Address: 55 1st St.; Phone: 888-333-4444

City: Brooklyn; State: New York; Address: 9 25th St.; Phone: 888-555-4848

Should display like this:

  • California
    • Los Angeles
      • 123 Main St.
      • 888-111-2222
    • San Jose
      • 55 1st St.
      • 888-333-4444
  • New York
    • Brooklyn
      • 9 25th St.
      • 888-555-4848

So far, I am using this code to grab the states in a list and it is working:

<ul>
<?php
global $wpdb;
$query = "SELECT meta_value, COUNT(post_id) as count FROM $wpdb->postmeta WHERE meta_key = 'state' GROUP BY meta_value ORDER BY meta_value;";
$states = $wpdb->get_results( $query );
foreach( $states as $state ) :
echo'<li>'; echo $state->meta_value; echo '</li>';
endforeach;
?>
</ul>

But this is of course only displaying:

  • California
  • New York

What I'm trying to figure out is how to query the rest of the data I'm looking for.

Thanks in advance!

Share Improve this question asked Oct 28, 2016 at 17:19 AlvinAlvin 231 silver badge5 bronze badges 1
  • 1 the problem with querying just the meta table is that you don't know the status of the posts they're attached to- you'll get meta for posts in the trash for example. – Milo Commented Oct 28, 2016 at 20:38
Add a comment  | 

2 Answers 2

Reset to default 1

A little example, based on get_posts and get_post_meta.

$locations = get_posts(
    array('post_type' => 'locations',
        'posts_per_page' => -1,
        'post_status' => 'publish'
    );
echo '<ul>';
foreach ($locations as $location) {
  $location_city = get_post_meta($location->ID, 'city', true);
  $location_state = get_post_meta($location->ID, 'state', true);
  $location_address = get_post_meta($location->ID, 'address', true);

  echo '<li>' . $location_city;
  echo '<li>' . $location_state . '</li>';
  echo '<li>' . $location_address . '</li>';
  echo '</li>';
}
echo '</ul>';

You'll need to adapt $args array in the get_posts to order by special value. With this way you loop through posts and retrieve their custom fields with get_post_meta.

Edit: You can add a meta_query array in the arguments for the query, to get or order. Custom field in WP_Query

Hope it helps.

I ended up solving my issue using this guide: http://www.tomoro.com.au/blog/sort-and-group-posts-by-custom-field-with-unique-headings-in-wordpress/

本文标签: phpEcho a hierarchical list of post data from custom fields