Last active
July 13, 2021 19:40
-
-
Save jorpdesigns/50de70f893e97f0607ee11e766e35c14 to your computer and use it in GitHub Desktop.
Function for querying pages based on custom category added by ACF
This file contains hidden or 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 | |
function pages_query_acf() { | |
// Parameters available here: https://developer.wordpress.org/reference/classes/wp_query/ | |
$args = array( | |
'post_type' => 'page', | |
'posts_per_page' => -1, | |
'order' => 'ASC', | |
'orderby' => 'menu_order', | |
'meta_key' => 'page_category', // page_category is a dropdown field added via ACF | |
'meta_value' => 'page_category_value' // page_category_value is a value from the dropdown field mentioned above | |
); | |
$pages = get_posts($args); | |
$output = ''; | |
if ( $pages ) { | |
foreach ($pages as $page) { | |
$output .= '<li><a href="'. get_the_permalink() .'">'. get_the_title() .'</a></li>'; | |
} | |
wp_reset_postdata(); | |
} else { | |
$output .= '<li>Coming Soon</li>'; | |
} | |
return '<div class="custom-page-loop"><ul>'. $output .'</ul></div>'; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment