Last active
August 29, 2015 14:23
-
-
Save jancbeck/fdd8f0c796778f6263d0 to your computer and use it in GitHub Desktop.
How to search for the title of a related custom post type in the WordPress admin using ACF Pro
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 if( function_exists('acf_add_local_field_group') ): | |
acf_add_local_field_group(array ( | |
'key' => 'group_558c7f75de508', | |
'title' => 'Stories', | |
'fields' => array ( | |
array ( | |
'key' => 'field_558c7fe4df0bc', | |
'label' => 'Select artist', | |
'name' => 'select_artist', | |
'type' => 'relationship', | |
'instructions' => '', | |
'required' => 0, | |
'conditional_logic' => 0, | |
'wrapper' => array ( | |
'width' => '', | |
'class' => '', | |
'id' => '', | |
), | |
'post_type' => array ( | |
0 => 'artist', | |
), | |
'taxonomy' => array ( | |
), | |
'filters' => array ( | |
0 => 'search', | |
1 => 'taxonomy', | |
), | |
'elements' => array ( | |
0 => 'featured_image', | |
), | |
'min' => '', | |
'max' => 1, | |
'return_format' => 'object', | |
), | |
), | |
'location' => array ( | |
array ( | |
array ( | |
'param' => 'post_type', | |
'operator' => '==', | |
'value' => 'story', | |
), | |
), | |
), | |
'menu_order' => 0, | |
'position' => 'normal', | |
'style' => 'default', | |
'label_placement' => 'top', | |
'instruction_placement' => 'label', | |
'hide_on_screen' => '', | |
)); | |
endif; |
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 | |
/* | |
Plugin Name: WPSX 185734: Admin search ACF relationship | |
Plugin URI: https://wordpress.stackexchange.com/questions/185734/admin-search-acf-relationship | |
Description: Demonstrates how to search for the title of a related custom post type in admin | |
Version: 1.0 | |
Author: Jan Beck | |
Author URI: http://jancbeck.com | |
License: GPL2 | |
License URI: https://www.gnu.org/licenses/gpl-2.0.html | |
Text Domain: acf-relationship-search | |
*/ | |
add_action( 'pre_get_posts', 'wpsx_185734_acf_search_relationship' ); | |
function wpsx_185734_acf_search_relationship( $q ) { | |
$screen = get_current_screen(); | |
$s = $q->get('s'); | |
$post_type = $q->get('post_type'); | |
// Be very selective when running code at the pre_get_posts hook | |
if ( ! is_admin() || empty( $s ) || ( isset( $screen->post_type ) && 'story' != $screen->post_type ) || 'story' != $post_type ) { | |
return; | |
} | |
// get all artists that match the search parameter $s | |
$found_artists = get_posts( array('post_type' => 'artist', 'nopaging' => true, 's' => $s, 'fields' => 'ids') ); | |
// build a meta query to include all posts that contain the matching artist IDs in their custom fields | |
$meta_query = array(); | |
foreach ( $found_artists as $artist_id ) { | |
$meta_query[] = array( | |
'key' => 'select_artist', // name of custom field | |
'value' => '"' . intval($artist_id) . '"', // matches exactly "123", not just 123. This prevents a match for "1234" | |
'compare' => 'LIKE' | |
); | |
} | |
$q->set( 'meta_query', $meta_query ); | |
$q->set( 's', '' ); // unset the original query parameter to avoid errors | |
} | |
add_action( 'init', 'wpsx_185734_register_customs' ); | |
function wpsx_185734_register_customs() { | |
$labels = array( | |
'name' => _x( 'Stories', 'post type general name', 'acf-relationship-search' ), | |
'singular_name' => _x( 'Story', 'post type singular name', 'acf-relationship-search' ), | |
'menu_name' => _x( 'Stories', 'admin menu', 'acf-relationship-search' ), | |
'name_admin_bar' => _x( 'Story', 'add new on admin bar', 'acf-relationship-search' ), | |
'add_new' => _x( 'Add New', 'story', 'acf-relationship-search' ), | |
'add_new_item' => __( 'Add New Story', 'acf-relationship-search' ), | |
'new_item' => __( 'New Story', 'acf-relationship-search' ), | |
'edit_item' => __( 'Edit Story', 'acf-relationship-search' ), | |
'view_item' => __( 'View Story', 'acf-relationship-search' ), | |
'all_items' => __( 'All Stories', 'acf-relationship-search' ), | |
'search_items' => __( 'Search Stories', 'acf-relationship-search' ), | |
'parent_item_colon' => __( 'Parent Stories:', 'acf-relationship-search' ), | |
'not_found' => __( 'No stories found.', 'acf-relationship-search' ), | |
'not_found_in_trash' => __( 'No stories found in Trash.', 'acf-relationship-search' ) | |
); | |
$args = array( | |
'labels' => $labels, | |
'public' => true, | |
'publicly_queryable' => true, | |
'show_ui' => true, | |
'show_in_menu' => true, | |
'query_var' => true, | |
'rewrite' => array( 'slug' => 'story' ), | |
'capability_type' => 'post', | |
'has_archive' => true, | |
'hierarchical' => false, | |
'menu_position' => null, | |
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ) | |
); | |
register_post_type( 'story', $args ); | |
$labels = array( | |
'name' => _x( 'Artists', 'post type general name', 'acf-relationship-search' ), | |
'singular_name' => _x( 'Artist', 'post type singular name', 'acf-relationship-search' ), | |
'menu_name' => _x( 'Artists', 'admin menu', 'acf-relationship-search' ), | |
'name_admin_bar' => _x( 'Artist', 'add new on admin bar', 'acf-relationship-search' ), | |
'add_new' => _x( 'Add New', 'artist', 'acf-relationship-search' ), | |
'add_new_item' => __( 'Add New Artist', 'acf-relationship-search' ), | |
'new_item' => __( 'New Artist', 'acf-relationship-search' ), | |
'edit_item' => __( 'Edit Artist', 'acf-relationship-search' ), | |
'view_item' => __( 'View Artist', 'acf-relationship-search' ), | |
'all_items' => __( 'All Artists', 'acf-relationship-search' ), | |
'search_items' => __( 'Search Artists', 'acf-relationship-search' ), | |
'parent_item_colon' => __( 'Parent Artists:', 'acf-relationship-search' ), | |
'not_found' => __( 'No artists found.', 'acf-relationship-search' ), | |
'not_found_in_trash' => __( 'No artists found in Trash.', 'acf-relationship-search' ) | |
); | |
$args = array( | |
'labels' => $labels, | |
'public' => true, | |
'publicly_queryable' => true, | |
'show_ui' => true, | |
'show_in_menu' => true, | |
'query_var' => true, | |
'rewrite' => array( 'slug' => 'artist' ), | |
'capability_type' => 'post', | |
'has_archive' => true, | |
'hierarchical' => false, | |
'menu_position' => null, | |
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ) | |
); | |
register_post_type( 'artist', $args ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment