Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save snrjosh/1f6ec03851ff885c3ab7f771368bb136 to your computer and use it in GitHub Desktop.
Save snrjosh/1f6ec03851ff885c3ab7f771368bb136 to your computer and use it in GitHub Desktop.
Filter posts on ACF Relationship field based on Options page
<?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