0

I have a Taxonomy called Cities. I have created a custom field for this taxonomy, using ACF, called Country So I can create a City and select the Country (select list with predefined options) it belongs to in the dashboard.

I then create my Post, and select a City it belongs to. (ie Paris)

On front end, I have created a drop down list from the value of the ACF custom field which works fine, like this

  $field_key = "field_1234564545445";
  $field = get_field_object($field_key);

  if( $field )
  {
          echo '<select name="country">';
          echo '<option value="">Choose Country</option>';
          foreach( $field['choices'] as $k => $v )
          {
              echo '<option value="' . $k . '">' . $v . '</option>';
          }
      echo '</select>';
  }

This gives me a list of the Country used by the Taxonomy Cities. So I get a list like France, Italy, Spain etc. No I am trying to query my post type using one of these country, but I can't manage to get a result.

$args = array( 
    'posts_per_page'    => '12',
    'post_type' => 'ARTICLES',
    'orderby' => 'date',
    'order' => ASC,
    'meta_query' => array(
          array(
           'key'      => 'country',//Also tried field_1234564545445
           'compare'  => '=',
           'value'    => $country,
        ),    
      ),
      'paged' => $paged,
    );

Will appreciate a pointer thx

4
  • So the custom field is connected to the taxonomy not the post? If so you might have to use tax_query.
    – ngearing
    Jun 8, 2015 at 11:38
  • Hi @Nath yes the field 'Country' is connected to the Taxonomy, Ho will I query the tax_query via its custom field?
    – wp supprt
    Jun 8, 2015 at 12:25
  • This doesn't answer your question exactly, but I wonder if an easier way to handle this would be to make the taxonomy hierarchal, with the country being parent terms and the cities being child terms. Then your query is a little more straightforward.
    – Josh M
    Jun 8, 2015 at 12:38
  • @Josh Yes i thought about this but it is not possible in my case, the country needs to stay as a custom field of the taxonomy
    – wp supprt
    Jun 8, 2015 at 12:42

1 Answer 1

1

Ok, I did it another way.

First, get all the terms for the taxonomy. If the term is equal to the value requested, put it into an array Then do the tax_query with this array.

Final

$argsc = array(
'order'             => 'ASC',
'hide_empty'        => true,
'fields'            => 'all', 
); 

$termsc = get_terms("cities", $argsc);
$cities_array = array( );
foreach ( $termsc as $cterm ) {
//Get country Customf ield for that term
$thecountry = get_field( 'country', $cterm );
 if($country == $thecountry){
 //if custom field == variable from select/search put in array
  array_push($cities_array,$cterm->name);
 }else{
 //do nothing
 }
}

ksort( $cities_array);//just sorting
$args = array(
    // Arguments for your query.
    'posts_per_page'    => '12',
    'post_type' => 'ARTICLES',
        'orderby' => 'date',
        'order' => ASC,
        'tax_query' => array(
                array(
                'taxonomy' => 'cities',
                'field'    => 'slug',
                'terms'    => $cities_array,
            ),

        ),
      'paged' => $paged,
    );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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