Support

Account

Home Forums Add-ons Flexible Content Field List of slugs from custom post taxonomy for loop

Solved

List of slugs from custom post taxonomy for loop

  • Hello

    I have a flexible content field in which I want to display certain events.
    The events that are shown should be selected by a taxonomy field where several entries (cities) of a custom post taxonomy (locations) can be selected by checkbox. (multiselect)
    The return is an Taxonomy object.
    I need a list of the slugs from the selected fields.
    The list should be e.g. like: ‘munich’, ‘london’,

    The relevant Code is

    <?php 
    $locations = get_sub_field('select_locations');
    $loop = new WP_Query( array( 'post_type' => 'events', 'posts_per_page' => 99, 
    	
    	
    		'tax_query' => array(
    			array(
    						'taxonomy' => 'locations',
    						'field' => 'slug',
    						'terms' => array(
    							/* HERE SHOULD BE THE LIST */
    						)
    					)
    		) 
    ) ); 
    
    ?>

    I tried several things but I can’t get it working.
    I would be very happy if someone could help me with this.

    Thanks a lot.

    • Helmet

    • March 6, 2020 at 1:27 pm

    I’ve been trying to do something similar. I needed to pull list of category Names and slugs and use them to populate a News feed. I’m sure this is probably not the best way to do it but it worked for me.

    	
    //vars
    $terms = get_sub_field('filter_news_feed');	
    	
    //set variables for the category slug and category name
    $output = "";
    $nameOutput = "";
    
    //Loop throuh each item in the array and assign the slug and name values to their variables 
    foreach( $terms as $term ):
    	$output.= $term->slug . ', ';
    	$nameOutput.= $term->name . ', ';
    endforeach; 
    
    //trim the last , and space ' ' off of the list
    $trimOutput = rtrim($output,', ');
    	
    //trim the last , and space ' ' off of the list
    $trimNameOutput = rtrim($nameOutput,', ');
    

    and then to load it into the array:

    
    $args = array(
      'post_type' => 'post',
      'post_parent' => 0,
      'posts_per_page' => 4,
      'order' => 'DSC',
    );
    					
    //adding 'category_name' => 'slug, slug, slug, etc'; to args array
    $args['category_name'] = $trimOutput;
    
    

    Hope that helps….

    • MythosM

    • March 24, 2020 at 1:36 am

    Hey Helmet,

    thank you for your answer. I allready found a solution with the help of a developer.
    It seems quite similar to yours.

    Just for anyone who’s interested:
    `
    <?php
    $locations = get_sub_field(‘orte_eingrenzen’);
    $locationsSelected = array();
    foreach( $orte as $ort ):
    $locationsSelected[] = $ort->slug;
    endforeach;

    $loop = new WP_Query( array( ‘post_type’ => ‘kurse’, ‘posts_per_page’ => 99, ‘meta_key’ => ‘datum’, ‘orderby’ => ‘meta_value’, ‘order’ => ‘ASC’,

    ‘tax_query’ => array(
    array(
    ‘taxonomy’ => ‘loacations’,
    ‘field’ => ‘slug’,
    ‘terms’ => $locationsSelected
    )
    )
    ) );
    ?>

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.