custom post types - Admin search ACF relationship

I have a custom post type name story. It has a custom field named 'select_artist' with a relationship to a cus

I have a custom post type name story. It has a custom field named 'select_artist' with a relationship to a custom post type named artist.

Within the story edit page, I want to use the search box to search for the 'select_artist' title. At the moment it will only return results when I search for the artist post ID, however I want to be able to search for the artist post title.

Any suggestions?

I have a custom post type name story. It has a custom field named 'select_artist' with a relationship to a custom post type named artist.

Within the story edit page, I want to use the search box to search for the 'select_artist' title. At the moment it will only return results when I search for the artist post ID, however I want to be able to search for the artist post title.

Any suggestions?

Share Improve this question edited Apr 29, 2015 at 4:12 Pieter Goosen 55.4k23 gold badges116 silver badges210 bronze badges asked Apr 28, 2015 at 22:20 bencbenc 1072 silver badges10 bronze badges 7
  • What you describe is exactly how the ACF relationship field should behave by default. You enter a post title, not ID for search. advancedcustomfields/resources/relationship – Jan Beck Commented Apr 29, 2015 at 0:12
  • @JanBeck I should have been clearer. The problem is on the search feature of the post edit page, not the search when setting up a new relationship. – benc Commented Apr 29, 2015 at 1:31
  • You mean this page right codex.wordpress/Posts_Screen#Table_of_Posts? – Jan Beck Commented Apr 29, 2015 at 10:55
  • @JanBeck That's the one, however for a custom post type. – benc Commented Apr 30, 2015 at 20:43
  • You will need to hook into pre_get_posts, get the list of post IDs of all custom posts with that keyword, then change the original query to include a meta query for the list of post IDs in the meta field. The ACF documentation has an article about it advancedcustomfields/resources/querying-relationship-fields. If all of this sounds like gibberish to you, let me know, and I'll add a more detailed answer. – Jan Beck Commented May 1, 2015 at 1:32
 |  Show 2 more comments

1 Answer 1

Reset to default 1

Here is a code snippet that will do what you described:

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 contains
  // 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
}

The code above assumes you have registered the custom post types and added an ACF relationship field named "select_artist" assigned to the story post type.

I created a public gist that also contains the code to register the custom post types and field group so it will run as a stand-alone-plugin: https://gist.github/jancbeck/fdd8f0c796778f6263d0

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744935323a4601984.html

相关推荐

  • custom post types - Admin search ACF relationship

    I have a custom post type name story. It has a custom field named 'select_artist' with a relationship to a cus

    2天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信