Support

Account

Home Forums Backend Issues (wp-admin) Changing query of Post Object field upon changing the field Reply To: Changing query of Post Object field upon changing the field

  • Hmm. After editing my reply, it somehow got deleted. So please delete this duplicate if it somehow gets back.

    Thank you @hube2!

    Okay, bear with me on this one. I’m aware that the way I retrieve the ID and the featured image is very hackish. So I’ll try to use a more ACF way of doing this eventually. But just to demonstrate my current problem. 🙂

    I have (for now) created something in PHP that echoes the ID and the featured image URL for each post that is selected in the Post Object fields into a hidden div in the Post Object title.

    function my_post_object_result($title, $post, $field, $post_id)
    {
        $post_thumbnail_url = get_the_post_thumbnail_url($post->ID);
    
        $hidden_div = $title;
        $hidden_div .= '<div id="' . $post->ID . '" class="featured-image" style="display:none;">' . $post_thumbnail_url . ' </div>';
    
        return $hidden_div;
    }
    
    add_filter('acf/fields/post_object/result', 'my_post_object_result', 10, 4);

    The featured image URL is used for creating a separate div underneath the Post Object input with the help of JS. As they are used for a grid, it’s important for the admin to see the featured image of the post they select. The ID attribute is then used by JS to create an array containing each post ID that is currently selected, and it’s then sent as the data through AJAX.

    This is what I’m doing in JS on page load:

    $(window).on("load", function() {
    
            $(".acf-field-post-object").each(function() {
                var imageURL = $(this).find(".select2-selection").find(".featured-image").text();
                $(this).find(".acf-input").append("<div class='featured-image-container'><div class='inner'><img src='" + imageURL + "' /></div></div>");
                var newPost = $(this).find(".select2-selection").find(".featured-image").attr("id");
                selectedPosts.push(newPost);
            })
    
            var filteredPosts = selectedPosts.filter(function(id){
            	return id != undefined
            })
            var data = {
                'action': 'my_action',
                'selectedPosts': filteredPosts,
            };
    
            $.post(ajax_object.ajax_url, data, function(response) {
                console.log(response);
            });
    
        })

    The response from AJAX is returning a PHP array with all the selected posts, which is what I want. The my_action function that the AJAX is sending the data to looks like this:

    
    add_action('wp_ajax_my_action', 'my_action');
    function my_action()
    {
        $postitems = $_POST['selectedPosts'];
        print_r($postitems);
        function work_post_object_query($args, $field, $post_id)
        {
            $args['post_status'] = array('publish');
            $args['post__not_in'] = $postitems;
    
            return $args;
        }
    
        // filter for every field
        add_filter('acf/fields/post_object/query', 'work_post_object_query', 10, 3);
        exit();
    }
    

    And this is where I’m experiencing trouble. Is this the correct way of using the filter with the AJAX data? I’m suspecting (apart from the hack I used in the beginning) that I’m doing something very wrong here.