Support

Account

Home Forums Backend Issues (wp-admin) Limit results in filter post_object query

Solved

Limit results in filter post_object query

  • Hello,

    I try to use the query filter on post_object, it’s okay to add args in the query, for example, adding [‘posts_per_page’] = 7;

    The goal is to show only 7 posts on the select field, but even with this arg, the list show all posts.

    (I try [‘numberposts’] too, same result).

    How can i limit results in a select field using post_object type.

    By the way thanks for this useful plugin.

  • Hi @romain9p

    Looking at the source code, you need to use the numberposts param.
    If that doesn’t work, perhaps the filter is not running correctly.

    Can you post your code?

    Thanks
    E

  • Here it is !

    I try numberposts whithout success, I don’t know if it’s important but for your information the select field list custom posts type.

    add_filter('acf/fields/post_object/query/key=*my_field_key*', 'change_posts_order', 10, 3);
    
    function change_posts_order( $args, $field, $post )
    {
    
        $args['numberposts'] = 7;
        $args['sort_order'] = 'DESC';
        $args['sort_column']  = 'date';
        $args['orderby'] = 'date';
        $args['order'] = 'date';
        $args['post_status'] = 'publish';
    
        return $args;
    }

    Thanks

  • Hi @romain9p

    Can you debug your filter by running this code before returning the $args:

    
    echo '<pre>';
    	print_r($args);
    echo '</pre>';
    die;
    

    Do you see the array printed?

  • Hi elliot,

    Here the $args

    Array
    (
        [numberposts] => 7
        [post_type] => emission
        [orderby] => date
        [order] => date
        [post_status] => publish
        [suppress_filters] => 
        [sort_column] => date
        [sort_order] => DESC
    )
  • How strange. Looks like the args are correctly being set,

    Your next step is to find out why the args aren’t being read in by ACF by running a simple debug in the ACF code.

    Edit the core/fields/post_object.php file and on line 171, can you print out the $args? Are the args taking ont he changes from your filter?

    Thanks
    E

  • Hi @elliot,

    This is strange, $args are ok if I print it on core/fields/post_object.php at line 171.

    Array ( [numberposts] => 7 [post_type] => emission [orderby] => date [order] => date [post_status] => publish [suppress_filters] => [sort_column] => date [sort_order] => DESC )

  • Hi @romain9p

    The only thing I can think of is that there is a filter running on the pre_get_posts which is overriding this setting.

    Hopefuly that points you in the right direction.

    Thanks
    E

  • I resolved it !

    I don’t know why but with my custom post type, in core/fields/post_object.php, it runs get_pages() function instead of get_posts() (maybe).

    So I change the $args['numberposts'] by $args['number'] et voila !

    Romain

  • I have a similar is issue when trying to do this:

    function my_post_object_query( $args, $field, $post )
    {
        // filter by the posts current categories
        $args['cat'] = implode(",", wp_get_post_categories($post->ID));
    	echo "<pre>";
    	print_r($args);	
    	echo "<pre>";
    	return $args;
    	
    }
    
    add_filter('acf/fields/post_object/query/key=field_524f02159316d', 'my_post_object_query', 10, 3);

    The category filter is not picked up… same as above, the post_object.php is using get_pages() against a custom post type.

    Suggestions other then changing the core file?

  • My Bad! … I had my custom post type set as “Hierarchical” change that to false and its good to go.

  • I just ran into this issue whereby filtering a post_object’s query wasn’t taking, but it looks like when a post_object’s type is ‘page’, ACF is effectively firing get_pages vs. creating a new WP_Query, so the args are all different.

    So, running this choked:

    	$args['post_parent'] = $parent->ID;
    	$args['orderby'] = 'menu_order';
    

    While running this works great:

    	$args['child_of'] = $parent->ID;
    	$args['sort_column'] = 'menu_order';
    

    Hopefully this will help anyone else who’s wondering why this isn’t working properly.

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

The topic ‘Limit results in filter post_object query’ is closed to new replies.