Support

Account

Home Forums Backend Issues (wp-admin) Post object option ignores a custom post type

Solving

Post object option ignores a custom post type

  • Hey there,

    I have a Post Object set up that filters by 5 post types: News, Events, Stories, Projects, and Page. However, when using this option field (as in editing a page and trying to select one of these posts) the “select” menu doesn’t populate the Projects post type (the <ul> that is made to look like a <select>). As I scroll down it continues to populate about 20 or so posts at a time before each AJAX request for the next set. but it never populates the Project post type.

    It doesn’t seem to matter what the combination of post types is either. I have tried dropping post types (except for Projects) out of this Post Object field and Projects still never get displayed. Even down to only two post types, like Pages and Projects – the only post type in the list is Pages.

    It also doesn’t seem to matter what order I put the post types in either, Projects always get ignored.

    However, I can get Projects to display in the “select” list if they are the only post type chosen. Of course, I need the other post types too…

    For your reference, here is a screenshot of this option’s settings: http://i.imgur.com/8TreJpw.png

    Here is a screenshot of the “select” menu: http://i.imgur.com/t3OB9oe.png
    I did modify the containers a bit to remove the max-height and increase the height a lot to show the entire list, but here it’s done populating and there are no Projects listed.

    I was watching for Javascript errors too, but as you can see from this screenshot of the console there were no errors: http://i.imgur.com/AP4APcl.png
    Those POST requests are either the “select” menu populating or the “wp-refresh-post-lock” thing. The list population calls always returned some normal-looking JSON like this:
    [{"text":"Page","children":[{"id":8630,"text":"- Downloads"},{"id":8628,"text":"- Events"},{"id":8624,"text":"- History"},{"id":8626,"text":"- News"},{"id":17577,"text":"Annual Offering Resources"},{"id":23830,"text":"Call to Listen"},{"id":12,"text":"Contact"},{"id":6,"text":"Impact"},{"id":8638,"text":"- Stories"},{"id":8640,"text":"- Videos"},{"id":8826,"text":"- - Making Waves"},{"id":8855,"text":"- - News Clips"},{"id":4,"text":"Listen"},{"id":17411,"text":"- FM Schedule"},{"id":8651,"text":"- Language"},{"id":8621,"text":"- Location"},{"id":8415,"text":"- Radio Schedule"},{"id":8707,"text":"Spread The Word"},{"id":8,"text":"Support"},{"id":8692,"text":"- Donate"}]}]

    And, just in case, here is the code that defines the ‘Project’ post type:

    ### Projects
    	$labels = array(
    		'name'               => 'Project',
    		'singular_name'      => 'Project',
    		'add_new'            => 'Add New',
    		'add_new_item'       => 'Add New Project',
    		'edit_item'          => 'Edit Project',
    		'new_item'           => 'New Project',
    		'all_items'          => 'All Projects',
    		'view_item'          => 'View Project Page',
    		'search_items'       => 'Search Projects',
    		'not_found'          => 'No Projects found',
    		'not_found_in_trash' => 'No Projects found in the Trash',
    		'parent_item_colon'  => '',
    		'menu_name'          => 'Projects'
    	);
    	$args = array(
    		'labels'        => $labels,
    		'description'   => 'Projects that people can donate to.',
    		'public'        => true,
    		'menu_position' => 20,
    		'hierarchical'	=> true,
    		'supports'      => array( 'title', 'editor', 'excerpt', 'thumbnail' ),
    		'has_archive'   => false,
    		'menu_icon'		=> 'dashicons-clipboard'
    	);
    	register_post_type( 'project', $args );

    Anyway, any help is greatly appreciated. If you need any more info let me know.

  • I’m having exactly the same problem. I have two post types: Post and Blog. The Post is renamed to News but it doesn’t affect because I have tried also without the renaming.

    If I’ll try to add both of those post types to the filter, only the Post post type is shown. If I try the Blog post type alone or with some other combination of post types (Media) it works correctly.

    I have the latest ACF installed (5.2.5)

  • Well, “glad” to see someone else had the same issue. ACF support tried helping me through email, but it was too late for the particular project I was working on.

    I was using the post object as a way for the client to choose a link for a “read more” button on a slider. The post object helps to reduce user error, that is, make it so the client had to select a post that exists on their website.

    I ended up having to use a text field (well, URL field) so the client can copy/paste a URL in. Wasn’t ideal, but it works.

  • Hi,

    I don’t know if we are sharing the same issue (and for me, the issue stretches further back than 5.2.5) but I’ve figured out mine.

    The Context:

    The project I’m currently working on uses Polylang for translations. Custom fields aren’t set to be translated, their data is synced manually.

    I have a post object setup to retrieve posts from two custom post types: partner and sponsor. These post types aren’t configured to be multilingual.

    The Issue:

    In acf_field_post_object::render_field(), if there’s a saved value, ACF will retrieve all posts matching the saved value (in order to pre-populate the rendered field).

    
    $posts = acf_get_posts(array(
    	'post__in' => $field['value']
    ));
    

    Unfortunately, this returns an empty array because acf_get_posts() will default to loading from all available post types. In my case, acf_get_post_types() returns: post, page, attachment, partner, and sponsor.

    And this is the key problem: post and page are configured to be multilingual. This triggers Polylang to add a condition to fetch posts in the current language which the non-multilingual post types can’t fulfill.

    First Solution:

    Require ACF to pass along the filtered list of post types:

    
    $posts = acf_get_posts(array(
    	'post__in' => $field['value'],
    	'post_type' => $field['post_type']
    ));
    

    The one downside of this is if the list of filtered post types changes (e.g., a post type is removed), it will affect any currently saved value assigned to that now-removed post type.

    Second Solution:

    Intercept Polylang before it adds that language condition to the WP_query. I accomplish this using a backtrace to figure out if the WP_query was called by acf_get_posts() by way of acf_field->render_field().

    **Updated 2015-05-21T17:22-05:00**

    
    add_action( 'pre_get_posts', function ( &$wp_query ) {
    	$is_acf_get_posts = (
    		   ( $e = new Exception )
    		&& ( $trace = $e->getTraceAsString() )
    		&& false !== strpos( $trace, 'acf_get_posts(Array)' )
    		&& (
    			   (   is_admin() && false !== strpos( $trace, '->render_field(Array)' ) )
    			|| ( ! is_admin() && false !== strpos( $trace, '->format_value(' ) )
    		)
    	);
    
    	if ( $is_acf_get_posts && pll_is_not_translated_post_type( $wp_query->get('post_type') ) ) {
    		pll_remove_language_query_var( $wp_query );
    	}
    }, 9 );
    
    function pll_is_not_translated_post_type( $post_type ) {
    	global $polylang;
    
    	if ( isset( $polylang ) ) {
    		$pll_post_types = $polylang->model->get_translated_post_types( false );
    
    		return ( is_array( $post_type ) && array_diff( $post_type, $pll_post_types ) || in_array( $post_type, $pll_post_types ) );
    	}
    
    	return false;
    }
    
    function pll_remove_language_query_var( &$query ) {
    	$qv = &$query->query_vars;
    
    	unset( $qv['lang'] );
    
    	if ( ! empty( $qv['tax_query'] ) ) {
    		foreach ( $qv['tax_query'] as $i => $tax_query ) {
    			if ( isset( $tax_query['taxonomy'] ) && 'language' === $tax_query['taxonomy'] ) {
    				unset( $qv['tax_query'][ $i ] );
    			}
    		}
    	}
    }
    

    I made this filter very verbose just to make sure the idea is understand. I’m using a much more compact version in my project. This function can also be easily translated to your multilingual-plugin flavour (e.g., WPML).

    Cheers,

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

The topic ‘Post object option ignores a custom post type’ is closed to new replies.