Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support Nithin – WPMU DEV Support

    (@wpmudevsupport11)

    Hi @soniamariano,

    now I need, for another select field, that the options are dynamically loaded from an existing (published) CPT titles. How can I achieve that?

    The workflow will be similar to the existing snippet you have mentioned. Instead of predefined options in the example:

    private $options = array(
                'value-1'    => 'Label 1',
                'value-2'    => 'Label 2',
                'value-3'    => 'Label 3',
            );

    You’ll need to dynamically call the CPT titles. Maybe you might find the following helpful:
    https://stackoverflow.com/questions/15508406/get-titles-of-all-custom-post-types-in-a-page-in-wordpress

    Kind Regards,
    Nithin

    Thread Starter sonmar

    (@soniamariano)

    Hi Nithin @wpmudevsupport11,

    Thank you for your time.

    This seems to be very simple to achieve and however I’m unable do get it working…

    I tried following your example but keep getting code errors that I don’t understand.

    I also tried just adding this

    		array(
    	     'post_type' => 'programmes',  
    	     'numberposts' => -1,  
    	     'orderby' => 'title',  
    	     'order' => 'ASC' 
    )

    but the select field shows me “programmes”,”-1″,”title” etc. as options.

    Any ideas on this?

    Plugin Support Nithin – WPMU DEV Support

    (@wpmudevsupport11)

    Hi @soniamariano ,

    Could you please try replacing the “wpmudev_forminator_before_form_render” function in the given snippet with the following and see whether it helps?

        public function wpmudev_forminator_before_form_render( $id ){
                
                if( $this->form_id != $id ){
                    return;
                }
                
                
    
                 $post_type_query  = new WP_Query(  
    			array (  
    				'post_type'      => 'programmes',  
    			)  
    		);   
    		$posts_array      = $post_type_query->posts;   
    		$post_title_array = wp_list_pluck( $posts_array, 'post_title', 'ID' );
    		
    				$i = 0;
    		                foreach( $post_title_array as $key => $value ){
    		                
                   		                $post_title[] = $value; 
    
            		                 array_push($this->options, $post_title[$i]);
    		                
    			                $i++;
    		                 }
    		                 
                add_filter( 'forminator_field_markup', array( $this, 'wpmudev_forminator_field_markup' ), 10, 2 );
    
            }

    Kind Regards,
    Nithin

    Thread Starter sonmar

    (@soniamariano)

    Hi Nithin @wpmudevsupport11,
    Thank you very much, it worked! Just had to add a 'posts_per_page' => -1,
    so that the field showed all the CPTs titles.
    I wish you the best!
    Kind regards,
    Sonia

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Select field with CPT titles as options’ is closed to new replies.