Support

Account

Home Forums Add-ons Options Page Using acf/fields/post_object/query filter on options page

Solved

Using acf/fields/post_object/query filter on options page

  • I’m trying to allow the client is able to select several posts to feature in a hero section on the front end. To do this I have created an options page, and assigned some Post Object fields to it.

    The problem is that there are thousands of posts on the site, and the Post Object field displays hundreds of (no title) (draft) choices. I want to reduce the list by filtering acf/fields/post_object/query to display published posts.

    The filter takes 3 parameters: filter( $args, $field, $post_id ). But since this field is on an options page, what should the 3rd parameter be? Here is what I want to do, but how do I make it work for the options page that has no $post_id?

    function my_post_object_query( $args, $field, $post_id ) {
    	
        // only published posts
        $args['post_status'] = 'publish';
    	
        // return
        return $args;
        
    }
    
    // filter for a specific field based on it's name
    //add_filter('acf/fields/post_object/query/name=my_post_object', 'my_post_object_query', 10, 3);
    
    

    Thank you

  • Thanks to the support team, I got this to work. You don’t need to reference $post_id at all. Here’s my code:

    add_filter('acf/fields/post_object/query', 'prefix_only_published_posts', 10, 3);
    /**
     * Display only published posts
     */
    function prefix_only_published_posts( $args, $field, $post_id ) {
    	
    	$args['post_status'] = 'publish';
    	$args['orderby'] = 'date';
    	$args['order'] = 'DESC';
    		
    	return $args;
    }
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Using acf/fields/post_object/query filter on options page’ is closed to new replies.