PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, February 8, 2022

[FIXED] How to filter ACF post object field/search by custom field?

 February 08, 2022     advanced-custom-fields, wordpress     No comments   

Issue

I have a front end ACF form - acf_form(). With a post object field. I want to limit the queried results by a custom field. The code below works, except for when i try to type in the search bar - a get the following errors: The errors I am getting are PHP errors in ajax response;

0: "PhpConsole\Handler->handleError()"

1: "strpos()"

2: "acf_order_by_search()"

3: "acf_field_post_object->get_ajax_query()"

4: "acf_field_post_object->ajax_query()"

5: "do_action('wp_ajax_acf/fields/post_object/query')"

Also getting "Undefined index: s" and "strpos(): Empty needle"

My code:

function filter_customer_doc_query( $args, $field, $post_id ) {
  $user_id_doc = get_current_user_id();
  $business_id_doc = get_field('user_business_id', 'user_' . $user_id_doc);
  $args = array(
    'posts_per_page' => 10,
    'post_type'     => 'customer',
    'meta_key'      => 'customer_business_id',
    'meta_value'    => $business_id_doc,
  );
  return $args;
}
add_filter('acf/fields/post_object/query/name=doc_customer_object', 'filter_customer_doc_query', 10, 3);

Solution

Solved it by replacing $args above with:

$args['meta_query'] = array(
    array(
        'key'       => 'customer_business_id',
        'value' => $business_id_doc,
        'compare' => '=',
    )
);


Answered By - Viktor Fonster
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing