Support

Account

Home Forums Front-end Issues Retrieve Posts by ACF Select box/choices values

Solving

Retrieve Posts by ACF Select box/choices values

  • Hello guys

    In what is going to be my first big WordPress website, I have created categories like Cars, Boats, Airplanes, Motorbikes etc. These are the standard WordPress Categories.

    In Advanced Custom Fields, I have created some more categories – in the form of choices (allowing multiple values), based on the brand of each of the above. For example, when the user makes a new post and selects the category cars, then the ‘vehicle_brand’ (Field Name) appears below the post with the choices like Opel, Citroen, Fiat, etc. In the ACF settings, this is like:
    1 : Opel
    2 : Citroen
    3 : Fiat
    4 : VW
    5 : Audi
    6 : Mercedes

    So I have a category-cars.php file, which gives me all the posts with a car category. What I want is to be able to retrieve on this page, only specific brands with a filter. For example retrive only the Opel cars, and have them also paginated like 9 per page. Problably I’d have to use something like the code elow for a single and specific value. But now how I can get the result I want, based on the user choice. meta_key should be an array somehow…? Because I know than vehicle_brand is an array (if it has multiple choices). If not it’s a single value like get_field(‘billiard_category’)[0]

    $args = array(
    ‘numberposts’ => -1,
    ‘post_type’ => ‘event’,
    ‘meta_key’ => ‘location’,
    ‘meta_value’ => ‘Melbourne’
    );

    Please help if you know. I couldn’t find this on the forum, or at least didn’t understand if I was looking something related to my issue.
    Thanks!

  • same my problem, but I need field using to choose the parent category only (without subcategory) and other field using to choose the child category only from the parent category that have been chose from the first field (parent category) in create custom post ..

  • To give an idea on the first question, this would require using WP_Query and adding a meta_query and a tax_query.

    
    $args = array(
      'post_type' => 'cars',
      'post_per_page' => 9,
      'meta_query' => array(
        'key' => 'brand',
        'value' => $vehicle_brand
      ),
      'tax_query' => array(
        'taxonomy' => 'catetory',
        'field' => 'name',
        'terms' => array($vehicle_type),
        'include_children' => false,
      ),
    );
    
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.