1

I made a little custom module for the Divi Builder which allows me to display a specific single post (from a custom post type). This works fine, but the options are not sorted correctly in the builder, but are sorted correctly when rendered on the page, why is that and how can I fix it? Here's my setup: To select the specific post, I placed a "select" field inside the module, according to the official elegantthemes documentation. The fields options come from the $options array. Here's the code:

public function get_fields() {
        $options = $this->get_post_options();

        return array(
            'selected_post_id' => array(
                'label'           => esc_html__('Label text', 'et_builder'),
                'type'            => 'select',
                'options'         => $options,
                'description'     => esc_html__('Desctiption text.', 'et_builder'),
                'default'         => 'none',
            ),
        );
    }

The $options array is generated as follows (notice sorting parameters):

private function get_post_options() {
    $args = array(
        'post_type'      => 'custom-post-type-slug',
        'posts_per_page' => -1,
        'orderby'        => 'title',
        'order'          => 'ASC'
    );

    $posts = get_posts($args);
    $options = array('none' => esc_html__('Wählen Sie einen Ansprechpartner', 'et_builder'));

    foreach ($posts as $post) {
        $options[$post->ID] = $post->post_title;
    }

    return $options;
}

In case someone needs to inspect the render function, here is the shortened version:

public function render($attrs, $content = null, $render_slug) {
    $selected_post_id = $attrs['selected_post_id'];
        
    $options = $this->get_post_options();
    
    // Generate select-field
    $select_html = '<select id="ansprechpartner-select" name="ansprechpartner-select">';
    foreach ($options as $value => $label) {
        $selected = ($value == $selected_post_id) ? ' selected="selected"' : '';
        $select_html .= '<option value="' . $value . '"' . $selected . '>' . $label . '</option>';
    }
    $select_html .= '</select>';
        
    $output = $select_html;

    return $output;
}

Result: Inside Divi Module: Options sorted by post creation date:

in backend/builder sorted by post creation date

In Frontend: Options sorted alphabetically as I wanted:

in frontend alphabetically sorted

1
  • 1
    This may need to be posted to the DIVI support. Interesting that it works on the front end but not in the editor though. I can't see a reason why it wouldn't. :-( Apr 2 at 13:57

1 Answer 1

0

Turns out, Divi cannot handle associative arrays, in my case with post id as key and post title as value. Using an indexed array solves the problem. Unfortunately, this is not mentioned anywhere in the documentation.

Thus I changed the code in the get_post_options() function from

foreach ($posts as $post) {
        $options[$post->ID] = $post->post_title;
    }

to

$index = 1;
    foreach ($posts as $post) {
        $options[$index] = $post->post_title;
        $index++;
    }

Why this is the case, I can't answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.