Support

Account

Home Forums Front-end Issues Make a list of cities from custom fields

Helping

Make a list of cities from custom fields

  • Hello,

    I have the following task to accomplish and don’t know where to start: I am working on a job offer website and I would like to list all cities where there are job openings in a sidebar widget/list. Like this:

    We currently offer jobs in: Berlin, Munich, Potsdam, Stuttgart

    I have the cities saved in every post that is a job offer so the date is there. What I need to do is query all published job posts, get the field with the city and list them. BUT I also need to list every city only once and don’t repeat them.

    Can anyone help me getting this done right?

    Thank you and regards,
    Arne

  • The best way to do this would be using a taxonomy for city instead of a text field.

    Or you can query the database directly.

    
    // replace city below with your meta key
    global $wpdb;
    $date = date('Y-m-d H:i:s');
    $query = 'SELECT DISTINCT meta_value
              FROM '.$wpdb->postmeta.' pm
              WHERE pm.meta_key = "city"
              ORDER BY pm.meta_value';
    $values = $wpdb->get_results($query, 'ARRAY_A');
    

    See documentation for $wpdb here https://codex.wordpress.org/Class_Reference/wpdb

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Make a list of cities from custom fields’ is closed to new replies.