Created
May 11, 2023 21:33
-
-
Save snrjosh/1f6ec03851ff885c3ab7f771368bb136 to your computer and use it in GitHub Desktop.
Filter posts on ACF Relationship field based on Options page
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 | |
/** | |
* Filter posts queried on ACF Relationship field based on Options page | |
* Filter : https://www.advancedcustomfields.com/resources/acf-fields-relationship-query/ | |
* $post_id : The Options page 'post_id' setting specified in acf_add_options_page | |
* | |
*/ | |
//Modify which posts are queried based on Options page | |
add_filter('acf/fields/relationship/query/name=field_name', 'modify_rel_field_post_query', 10, 3); | |
function modify_rel_field_post_query( $args, $field, $post_id ) { | |
if( 'options_1' == $post_id ) { | |
$args['post_type'] = array('post'); | |
return $args; | |
}elseif( 'options_2' == $post_id ) { | |
$args['post_type'] = array('page'); | |
return $args; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment