Skip Navigation

[Resolved] Populate select field with list of posts in a custom post type – only showing 5

This support ticket is created 6 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

Tagged: 

This topic contains 6 replies, has 3 voices.

Last updated by Johan Marneweck 6 years, 1 month ago.

Assisted by: Waqar.

Author
Posts
#1156240

I have used this same code on my site:

https://toolset.com/forums/topic/populate-select-field-with-list-of-posts-in-a-custom-post-type/

to create a dropdown field that displays custom post types within the field, but only 5x items appear in the dropdown and there should be 9. No matter what I do it only shows the last 5 custom posts.

Link to a page where the issue can be seen: hidden link (day 1, day 2 fields etc. show this and you need to be logged in)

#1156243

This is the code I was referring to.

add_filter( 'wpt_field_options', 'populate_select_1_1', 10, 3);
function populate_select_1_1( $options, $title, $type ){
    switch( $title ){
        case 'Person 1 Day 1 - Selection':
            $options = array();
            $args = array(
                'post_type'        => 'recipe',
                'post_status'      => 'publish',
				'tax_query' => array(
        			array (
					'taxonomy'	   => 'recipe-category',
					'field' => 'slug',           // term_id, slug or name
            		'terms' => 'this-weeks-recipes'
                 	)
				)
			);
            $posts_array = get_posts( $args );
            foreach ($posts_array as $post) {
                $options[] = array(
                    '#value' => $post->ID,
                    '#title' => $post->post_title,
                     );
                }
                 
                break;
}
    return $options;
}
#1156387

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - I simply am not able to access the link you share and when I try to load it, it displays error on the browser:

ERR_CONNECTION_TIMED_OUT

Could you please send me working site as well as double check that access details you send me for wp-admin working.

#1156837

Hi, our server does not like incoming connections from India. Please use a proxy or get another supporter from a different area to try.

#1157555

Hi, Is there anyone who can help?

#1157615

Hi Johan,

Thank you for waiting, while I looked into the details you've shared.

By default, the "get_posts" function ( https://codex.wordpress.org/Template_Tags/get_posts ), returns 5 results, when "numberposts" or "posts_per_page" parameter is not set.

To make sure that all posts matching the criteria are used for the drop-down options, you can include "'posts_per_page' => -1," in your code:


add_filter( 'wpt_field_options', 'populate_select_1_1', 10, 3);
function populate_select_1_1( $options, $title, $type ){
    switch( $title ){
        case 'Person 1 Day 1 - Selection':
            $options = array();
            $args = array(
                'post_type'        => 'recipe',
                'posts_per_page'   => -1,
                'post_status'      => 'publish',
                'tax_query' => array(
                    array (
                    'taxonomy'     => 'recipe-category',
                    'field' => 'slug',           // term_id, slug or name
                    'terms' => 'this-weeks-recipes'
                    )
                )
            );
            $posts_array = get_posts( $args );
            foreach ($posts_array as $post) {
                $options[] = array(
                    '#value' => $post->ID,
                    '#title' => $post->post_title,
                     );
                }
                  
                break;
}
    return $options;
}

Note: I wasn't able to access your website's admin area, through VPN/proxy from Asia or US regions. It only intermittently worked from UK region, but constantly showed connection timeout errors. For this reason, it doesn't seem like a geolocation issue and you can get in touch with your hosting's support team for a potential security or performance restrictions.

I hope this helps and please let me know how it goes.

regards,
Waqar

#1157891

My issue is resolved now. Thank you!