Search code examples
wordpresswordpress-themingcustom-wordpress-pageselementor

How can i display custom post type **posts** list in elementor select control


How can i display custom post type posts list in elementor select control

This is my code => I have used this but this shows post categories not post title

$options = array();

    $posts = get_posts( array(
        'post_type'  => 'digital_card'
    ) );

    foreach ( $posts as $key => $post ) {
setup_postdata( $post );
$options[$post->ID] = get_the_title();
    }
    
        
        $this->add_control(
            'show_elements',
            [
                'label' => __( 'Select Posts', 'plugin-domain' ),
                'label_block' => ('bool'),
                'type' => \Elementor\Controls_Manager::SELECT,
                'multiple' => true,
                'options' => $options,
                
            ]
        );

Solution

  • Try the below code.

    $options = array();
    
    $posts = get_posts( array(
        'post_type'  => 'digital_card'
    ) );
    
    foreach ( $posts as $key => $post ) {
        $options[$post->ID] = get_the_title($post->ID);
    }