Set Default and Min Value

User
$this->add_control(
'number_of_posts',
[
'label' => __('Number of Jobs', 'rosemeier'),
'type' => E lementorControls_Manager::NUMBER,
'default' => __( 'Default Title', 'sage' ),
]
);
add default value 10 and min 5
Assistant
To set a default value of `10` and a minimum value of `5` for the `number_of_posts` control in your Elementor widget, you can adjust the control definition as follows:

### Updated Code

$this->add_control(
    'number_of_posts',
    [
        'label' => __('Number of Jobs', 'rosemeier'),
        'type' => ElementorControls_Manager::NUMBER,
        'default' => 10, // Set default value to 10
        'min' => 5,      // Set minimum value to 5
    ]
);

### Explanation:
– **`default`**: Sets the default value to `10` instead of `'Default Title'`.
– **`min`**: Ensures the user cannot select a value lower than `5`.

This control will now show a default value of `10` and restrict the minimum value to `5` when adjusted in the Elementor editor.

User
$args = array(
'post_type' => 'resource',
'orderby' => 'title',
'order' => 'ASC',
);

$query = new WP_Query($args);
pass number_posts

Assistant
To pass the `number_of_posts` control value to your `WP_Query`, you can modify the code as follows. Assuming you're using the value from the Elementor control, you'll need to retrieve it using `$settings` or a similar variable that stores the control values.

### Updated Code

// Get the value of 'number_of_posts' from the Elementor settings.
$number_of_posts = !empty($settings['number_of_posts']) ? $settings['number_of_posts'] : 10;

// Define the WP_Query arguments.
$args = array(
    'post_type'      => 'resource',
    'posts_per_page' => $number_of_posts, // Use the control value here.
    'orderby'        => 'title',
    'order'          => 'ASC',
);

// Execute the query.
$query = new WP_Query($args);

### Explanation:
– **`$settings['number_of_posts']`**: Retrieves the value set in the Elementor control.
– **`posts_per_page`**: Limits the number of posts retrieved by the query.

### Handling Defaults:
– If the control value is not set, a default of `10` is used.
– Ensure that your Elementor widget code properly passes the `$settings` array to where you're running this query.

Make sure to replace `$settings` with the actual variable used in your Elementor widget class if it's named differently.

Leave a Comment

Your email address will not be published. Required fields are marked *