Support

Account

Home Forums General Issues retrieving a list of post titles with a certain ACF value

Solving

retrieving a list of post titles with a certain ACF value

  • Hi everyone.
    Newbie user here with a whole life ahead to learn proper coding…
    I recently purchased MapSVG plugin to use on my site and I need to achieve something I think it should not be very hard to code, but I don’t even know where to start.
    I have around 200 posts on my WP site with titles that correspond to the names of educational courses that have been giving around the world. Each posts contain several ACF, one of which is the country where the course has taken place. What I need is to be able to click on a country (say France for example) with the MapSVG plugin and have that region dynamically pull a list with the titles of ALL posts that have the ACF Location field with the value “France”.

    I know the basics behind the whole procedure, but have no idea of how to implement it- nor where to place any of the coded files… I understand I must query all posts where ACF_Location=France, determine the post titles and pass those into a new variable formatted to show a line break so it is read as a list.

    I also understand this procedure must be called as an action when the user clicks the country on the map, and the resulting list must be passed into the region detail popover…

    Would really love for someone to assist me here.
    THank you much

    Hektor

  • Not sure if this is what you’re after:
    This goes in your template:

    <?php 
    // args
    $args = array(
    	'posts_per_page'	=> -1,
    	'post_type'		=> 'your_cpt',
    	'meta_key'		=> 'location',
    );
    
    // query
    $the_query = new WP_Query( $args );
    
    if( $the_query->have_posts() ):
    	$countries = array();
    	while( $the_query->have_posts() ) : $the_query->the_post();
    		$countries[] = get_field('country');
    	endwhile;
    endif; 
    wp_reset_query();	 // Restore global post data stomped by the_post().
    
    // remove duplicates
    $country_list = array_unique($countries);
    
    if( $country_list ):
    	foreach( $country_list as $country ): ?>
    	<p><a href="#" class="country" country="<?php echo $country; ?>"><?php echo $country; ?></a></p>
    	<?php endforeach;
    endif;
    ?>
    <div id="ajax_countries_results"></div><!-- /ajax results -->
    

    Either add to your footer or enqueue the script

    
    <script>
    //filter the case studies
    jQuery(function($){
    	$('.country').click(function(){
    		
    		var country = jQuery(this).attr("country");		
    		
    		$.ajax({
    			type        : "POST",
    			data		: { action : 'get_country_posts', country: country }, 
    			dataType	: "html",
    			url			: '<?php echo admin_url('admin-ajax.php');?>', 
    
    			success     : function(data) {
    				//alert(this.data);
    				jQuery("#ajax_countries_results").html(data);
    				//console.log("success! country: " + country);
    			},
    			error       : function(xhr, status, error) {
    				var err = eval("(" + xhr.responseText + ")");
    				alert(err.Message);
    			}
    		});
    		return false;
    	});
    });
    </script>
    

    Then in your functions file

    
    <?php
    function get_country_posts() {
    	
    	$country = $_POST['country'];
    	
    	$args = array(
    			'post_type' 		=> 'your_cpt',
    			'post_status'		=> 'publish',
    			'posts_per_page'	=> -1, // show all posts.
    			'order'				=> 'DESC',
    			'meta_key'			=> 'location',
    			'meta_value'		=> $country	
    	);
    			
    	
    	$query = new WP_Query( $args );
    	if( $query->have_posts() ) : 
    
    		while( $query->have_posts() ): $query->the_post(); ?>
    		<p><?php the_title(); ?></p>
    		<?php endwhile;  wp_reset_postdata();
    
    	endif;
    	
    	die();
    
    }
    // Fire AJAX action for both logged in and non-logged in users
    add_action('wp_ajax_get_country_posts', 'get_country_posts');
    add_action('wp_ajax_nopriv_get_country_posts', 'get_country_posts');

    You need to amend the custom post type and your ACF fields but may help get you underway

    Code is untested!

  • Hi again Jarvis.
    You are very kind to, once again, give a hand.
    This all looks incredibly complex for my understanding though…
    I am using a plugin called CodeSnippets to centralize all of my php codes so it doesn’t have to go on my functions.php template… not sure what of all that would go there..
    I thought I would just need one function that does all the querying on my posts and feeds the output to the {{shortcode_inline ‘[thefunction country=”{{title}}”]’}} within the MapSVG region details view template…
    I will try to decipher this with someone that has much more experience than I do in coding!

    Kindest Regards,
    H├ęctor

  • Hi @hektorperez

    It really depends on how MapSVG outputs the info (I’m not familiar).

    I’ve done similar before but clicking the map then loaded the info on the same page (hence my example).
    You could just look to load a new page but pass a variable in the URL, then GET the variable and pass that into your query.

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

You must be logged in to reply to this topic.