Created
September 5, 2018 18:17
-
-
Save Garconis/1abd70e5be264696e9639aaae9fc4f78 to your computer and use it in GitHub Desktop.
WordPress | Loop for displaying only certain posts with certain ACF content and taxonomy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// create shortcode to list all addresses of a "Location" Page Type | |
add_shortcode( 'fs-location-address-loop', 'fs_sc_location_address_loop' ); | |
function fs_sc_location_address_loop( $atts ) { | |
ob_start(); | |
// define attributes and their defaults | |
extract( shortcode_atts( array ( | |
'post_type' => 'page', | |
'posts_per_page' => -1, | |
'type' => 'location', | |
), $atts ) ); | |
// define query parameters based on attributes above | |
$options = array( | |
'post_type' => $post_type, | |
'post_status' => 'publish', // only get posts with this status | |
// only get items that don't have an empty City | |
'meta_key' => 'address_city', | |
'meta_value' => array(''), | |
'meta_compare' => 'NOT IN', | |
// only grab parent pages | |
'post_parent' => 0, | |
'posts_per_page' => $posts_per_page, | |
// only grab items with this taxonomy and term | |
'tax_query'=> array( | |
array ( | |
'taxonomy' => 'fs_page_type', | |
'field' => 'slug', | |
'terms' => $type, | |
) | |
), | |
// get items with the following meta (state AND city AND not hidden in footer) | |
'meta_query' => array( | |
'relation' => 'AND', | |
'address_state_clause' => array( | |
'key' => 'address_state', | |
'compare' => 'EXISTS', | |
), | |
'address_city_clause' => array( | |
'key' => 'address_city', | |
'compare' => 'EXISTS', | |
), | |
// only get items that have are set to NOT be hidden in footer | |
'hide_in_footer_clause' => array( | |
'key' => 'hide_in_footer', | |
'value' => '1', | |
'compare' => '!=' | |
), | |
), | |
// orderby the state value first, then the city value | |
'orderby' => array( | |
'address_state_clause' => 'ASC', | |
'address_city_clause' => 'ASC', | |
), | |
); | |
$query = new WP_Query( $options ); | |
// run the loop based on the query | |
if ( $query->have_posts() ) { | |
echo'<ul class="footer-location-listing">'; | |
while ( $query->have_posts() ) : $query->the_post(); | |
$address = get_field('address'); | |
$phone = get_field('phone'); | |
echo '<li>'; | |
echo '<a href="'. get_permalink() .'"><h6>'; | |
if( $address['city'] && $address['state'] ) { | |
echo $address['city'] . ', '. $address['state']['value']; | |
} | |
else { | |
echo get_the_title(); | |
} | |
echo '</h6></a>'; | |
if($address['address_name']) { | |
echo '<span>' . $address['address_name'] . '</span><br>'; | |
} | |
if($address['address_line_1']) { | |
echo '<span>' . $address['address_line_1'] . '</span><br>'; | |
} | |
if($address['address_line_2']) { | |
echo '<span>' . $address['address_line_2'] . '</span><br>'; | |
} | |
if($address['city']) { | |
echo '<span>' . $address['city'] . '</span>, '; | |
} | |
if($address['state']) { | |
echo '<span>' . $address['state']['value'] . '</span> '; | |
} | |
if($address['zip_code']) { | |
echo '<span>' . $address['zip_code'] . '</span>'; | |
} | |
if($phone) { | |
echo '<br><span>Phone: ' . $phone . '</span>'; | |
} | |
echo '</li>'; | |
endwhile; | |
wp_reset_postdata(); | |
echo '</ul>'; | |
$myvariable = ob_get_clean(); | |
return $myvariable; | |
} | |
else { | |
echo'<ul class="local-listing">'; | |
echo '<li>'; | |
echo 'Sorry, no pages exist for this.'; | |
echo '</li>'; | |
echo '</ul>'; | |
$myvariable = ob_get_clean(); | |
return $myvariable; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment