Home › Forums › General Issues › search specific ACF field in admin/Posts
Hi
i have come across this code in order to search specific ACF field/fields in admin posts:
i have inserted the code into my function.php and works fine
only problem with it that it is displaying all ACF fields in the drop down list which i want to display only some specific fields such as: source, source author, translated by and so on.
can anyone help modify the code as per my needs?
Instead of doing the query to get a list of all avialble fields, construct an array with the field that you want to search.
$fields = array(
'field_1',
'field_2',
'field_3',
'field_4',
'etc...'
);
then alter the code used to display the field to use this new array
foreach ($fields as $field) {
printf('<option value="%s"%s>%s</option>', $field, $field == $current? ' selected="selected"':'', $field);
}
Hi John
thanks for the reply.
so my code bellow how it will be after adding the codes you have in your reply?
could you please reply with the full code as per you early reply.
add_filter( 'parse_query', 'ba_admin_posts_filter' );
add_action( 'restrict_manage_posts', 'ba_admin_posts_filter_restrict_manage_posts' );
function ba_admin_posts_filter( $query )
{
global $pagenow;
if ( is_admin() && $pagenow=='edit.php' && isset($_GET['ADMIN_FILTER_FIELD_NAME']) && $_GET['ADMIN_FILTER_FIELD_NAME'] != '') {
$query->query_vars['meta_key'] = $_GET['ADMIN_FILTER_FIELD_NAME'];
if (isset($_GET['ADMIN_FILTER_FIELD_VALUE']) && $_GET['ADMIN_FILTER_FIELD_VALUE'] != '')
$query->query_vars['meta_value'] = $_GET['ADMIN_FILTER_FIELD_VALUE'];
}
}
function ba_admin_posts_filter_restrict_manage_posts()
{
global $wpdb;
$sql = 'SELECT DISTINCT meta_key FROM '.$wpdb->postmeta.' ORDER BY 1';
$fields = $wpdb->get_results($sql, ARRAY_N);
?>
<select name="ADMIN_FILTER_FIELD_NAME">
<option value=""><?php _e('Filter By Custom Fields', 'baapf'); ?></option>
<?php
$current = isset($_GET['ADMIN_FILTER_FIELD_NAME'])? $_GET['ADMIN_FILTER_FIELD_NAME']:'';
$current_v = isset($_GET['ADMIN_FILTER_FIELD_VALUE'])? $_GET['ADMIN_FILTER_FIELD_VALUE']:'';
foreach ($fields as $field) {
if (substr($field[0],0,1) != "_"){
printf
(
'<option value="%s"%s>%s</option>',
$field[0],
$field[0] == $current? ' selected="selected"':'',
$field[0]
);
}
}
?>
</select> <?php _e('Value:', 'baapf'); ?><input type="TEXT" name="ADMIN_FILTER_FIELD_VALUE" value="<?php echo $current_v; ?>" />
<?php
}
add_filter( 'parse_query', 'ba_admin_posts_filter' );
add_action( 'restrict_manage_posts', 'ba_admin_posts_filter_restrict_manage_posts' );
function ba_admin_posts_filter( $query )
{
global $pagenow;
if ( is_admin() && $pagenow=='edit.php' && isset($_GET['ADMIN_FILTER_FIELD_NAME']) && $_GET['ADMIN_FILTER_FIELD_NAME'] != '') {
$query->query_vars['meta_key'] = $_GET['ADMIN_FILTER_FIELD_NAME'];
if (isset($_GET['ADMIN_FILTER_FIELD_VALUE']) && $_GET['ADMIN_FILTER_FIELD_VALUE'] != '')
$query->query_vars['meta_value'] = $_GET['ADMIN_FILTER_FIELD_VALUE'];
}
}
function ba_admin_posts_filter_restrict_manage_posts()
{
$fields = array(
'field_1',
'field_2',
'field_3',
'field_4',
'etc...'
);
?>
<select name="ADMIN_FILTER_FIELD_NAME">
<option value=""><?php _e('Filter By Custom Fields', 'baapf'); ?></option>
<?php
$current = isset($_GET['ADMIN_FILTER_FIELD_NAME'])? $_GET['ADMIN_FILTER_FIELD_NAME']:'';
$current_v = isset($_GET['ADMIN_FILTER_FIELD_VALUE'])? $_GET['ADMIN_FILTER_FIELD_VALUE']:'';
foreach ($fields as $field) {
printf('<option value="%s"%s>%s</option>', $field, $field == $current? ' selected="selected"':'', $field);
}
?>
</select> <?php _e('Value:', 'baapf'); ?><input type="TEXT" name="ADMIN_FILTER_FIELD_VALUE" value="<?php echo $current_v; ?>" />
<?php
}
@hube2 can we implement the same in the users. I am using Ultimate membership plugin. so in that we get separate users tab which shows all the uses present. so i wan this feature to be used over there as well. can you help me out how and what will the changes that will take place in the above code.
please i am stuck on this for a while now.
Thank you
I don’t have the answer to that. I do not know if the same filters run on the user admin page.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.