Support

Account

Home Forums Backend Issues (wp-admin) Post Object filter on edit Taxonomy page

Solving

Post Object filter on edit Taxonomy page

  • Hi there,

    first of loving the plugin, recently purchased the repeater fields add-on and works really well.

    I have a quick question and had a scout about and can’t find a solution on google 🙁

    I have a custom post type (Projects) and three custom taxonomies (Sector, Client and Service).
    I have a checkbox on the “Client” taxonomy page to toggle “featured” client. When the client is a featured client conditional field is shown. The conditional field is a post_object field.

    I want to use the add_filter('acf/fields/post_object/query/name=excluded_projects', 'mo_acf_available_posts', 10, 3); filter to restrict the post objects listed to only those with the term you are currently editing.

    For example:

    We have clients a, b and c.
    Each client has 3 projects (a1, a2, a3 etc).
    On the edit page for client a I click the featured client button, at the moment all projects are shown in the dropdown (a1-3, b1-3, c1-3) I only want projects from client a to show up.

    I just need the term id/slug for the active taxonomy term then the rest is pretty simple but can’t work out how to get this. I could strip the url for the $_GET['tag_ID'] but don’t know how to pass this to the filter.

    The reason for this is we have a featured clients page that takes the featured image from every project within the client term, however we want to hide some, so rather than having to separate taxonomies thought this might be a nice way to exclude certain posts from the archive page.

    It will still work without the filter but it’s just to simplify things.

    Any advice would be great, thanks.

    Danny Wilson

  • Hi @iamdanny

    You don’t need to ‘pass’ anything to the filter. From within the filter, you can access the $_GET array and find the term_id value.

    Thanks
    E

  • Hi Elliot,

    I’ve been having a look and don’t know if I’ve got something wrong with it but can’t access the global $current_screen, get_current_screen() returns null and the $_GET array is empty when I run them from within the filter. The $post term passed to the filter is also null.

    I’m going round in circles a bit here.

    function mo_acf_available_posts( $args, $field, $post )
    {
    	global $wp_query;
    	global $page_now;
    	$term = '1'; // WORK OUT HOW TO GET ACTIVE TERM
        $args['client'] = $term;
    	var_dump($args);
    	echo "<hr>";
    	var_dump($field);
    	echo "<hr>";
    	var_dump($wp_query);
    	echo "<hr>";
    	var_dump($page_now);
    	echo "<hr>";
    	var_dump($_GET);
    	echo "<hr>";
        return $args;
    }
    //add_filter('acf/fields/post_object/query/name=excluded_projects', 'mo_acf_available_posts', 10, 3);

    This filters the posts by the client term id=1, I just need to get the current term ID and exchange the hard-coded 1 in.

    Here’s the vars dumped by the server for each:

    $args:
    array(7) { ["numberposts"]=> int(-1) ["post_type"]=> string(7) "project" ["orderby"]=> string(5) "title" ["order"]=> string(3) "ASC" ["post_status"]=> array(5) { [0]=> string(7) "publish" [1]=> string(7) "private" [2]=> string(5) "draft" [3]=> string(7) "inherit" [4]=> string(6) "future" } ["suppress_filters"]=> bool(false) ["client"]=> string(1) "1" }

    $field:
    array(18) { ["key"]=> string(19) "field_52f3bb5c778f0" ["label"]=> string(17) "Excluded Projects" ["name"]=> string(27) "fields[field_52f3bb5c778f0]" ["_name"]=> string(17) "excluded_projects" ["type"]=> string(6) "select" ["order_no"]=> int(1) ["instructions"]=> string(0) "" ["required"]=> int(1) ["id"]=> string(27) "acf-field-excluded_projects" ["class"]=> string(11) "post_object" ["conditional_logic"]=> array(3) { ["status"]=> int(1) ["rules"]=> array(1) { [0]=> array(3) { ["field"]=> string(19) "field_52f3b64ed5d5c" ["operator"]=> string(2) "==" ["value"]=> string(1) "1" } } ["allorany"]=> string(3) "all" } ["post_type"]=> array(1) { [0]=> string(7) "project" } ["taxonomy"]=> array(1) { [0]=> string(3) "all" } ["allow_null"]=> int(0) ["multiple"]=> int(1) ["field_group"]=> int(149) ["value"]=> string(0) "" ["choices"]=> array(0) { } }

    $wp_query:
    object(WP_Query)#158 (51) { ["query"]=> NULL ["query_vars"]=> array(0) { } ["tax_query"]=> NULL ["meta_query"]=> bool(false) ["date_query"]=> bool(false) ["queried_object"]=> NULL ["queried_object_id"]=> NULL ["request"]=> NULL ["posts"]=> NULL ["post_count"]=> int(0) ["current_post"]=> int(-1) ["in_the_loop"]=> bool(false) ["post"]=> NULL ["comments"]=> NULL ["comment_count"]=> int(0) ["current_comment"]=> int(-1) ["comment"]=> NULL ["found_posts"]=> int(0) ["max_num_pages"]=> int(0) ["max_num_comment_pages"]=> int(0) ["is_single"]=> bool(false) ["is_preview"]=> bool(false) ["is_page"]=> bool(false) ["is_archive"]=> bool(false) ["is_date"]=> bool(false) ["is_year"]=> bool(false) ["is_month"]=> bool(false) ["is_day"]=> bool(false) ["is_time"]=> bool(false) ["is_author"]=> bool(false) ["is_category"]=> bool(false) ["is_tag"]=> bool(false) ["is_tax"]=> bool(false) ["is_search"]=> bool(false) ["is_feed"]=> bool(false) ["is_comment_feed"]=> bool(false) ["is_trackback"]=> bool(false) ["is_home"]=> bool(false) ["is_404"]=> bool(false) ["is_comments_popup"]=> bool(false) ["is_paged"]=> bool(false) ["is_admin"]=> bool(false) ["is_attachment"]=> bool(false) ["is_singular"]=> bool(false) ["is_robots"]=> bool(false) ["is_posts_page"]=> bool(false) ["is_post_type_archive"]=> bool(false) ["query_vars_hash"]=> bool(false) ["query_vars_changed"]=> bool(true) ["thumbnails_cached"]=> bool(false) ["stopwords":"WP_Query":private]=> NULL }
    This doesn’t look right, lots of false values and no information, think this might be why I can’t get the $_GET var.

    $pagenow:
    NULL

    $_GET:
    array(0) { }

    I may well be doing something stupid here but thought I’d post up the code in case someone else can see what I need to do, any advice would be great. Thanks,
    Danny

  • Hi @iamdanny

    Thanks for the logging.

    I can’t explain why your data is not correct. It’s almost as if the filter is being run via AJAX and has no idea of the page you are viewing. This is not possible as the post_object field (in v4) does not use AJAX.

    You can see that on line 168 of the ‘core/fields/post_object.php’ file, the 3 filters are applied.

    Would you mind doing some $_GET debugging in there?

    Thanks
    E

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Post Object filter on edit Taxonomy page’ is closed to new replies.