A
A
Aidar2016-01-22 14:47:07
WordPress
Aidar, 2016-01-22 14:47:07

How to pass value of selected checkbox to wordpress function array?

Hello! I'm trying to write the first plugin for wordpress (for myself). And here is the hitch with passing the variable to the array from the selected checkbox.
There is an array:

$posts = get_posts(array(
        'post_type'  => array('post','page')			
    ));

Here it is clear that we are receiving data and working with wordpress posts and pages. But here I thought that it is not always convenient to get into the code and change the array (when only pages are needed), but to do this using variables or substituting the value of the selected checkbox.
Made checkboxes:
<input type="checkbox" id="show_pages" value="page" /> Страницы
<input type="checkbox" id="show_posts" value="post" /> Записи

I don't know how to join them further with the array.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Vorotnev, 2016-01-24
@HeadOnFire

Settings API .
Well, or quite simply - checkboxes in the form, when submitting the form, do update_option( 'option_name' ). Then your output code will be like this:

$types = get_option( 'option_name' );
$posts = get_posts(array(
    'post_type'  => $types,
));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question