Multiple Select value saving Issue - php

i make meta box and in this i make select option with multiple
$opt_meta_author = get_post_meta($post->ID, 'opt_meta_author', true);
<select name="opt_meta_author" id="opt_meta_author" multiple="multiple">
<?php
$auth_args = array(
'post_type' => 'author',
'orderby' => 'title',
'order' => 'ASC'
);
$authors = new WP_Query($auth_args);
while($authors->have_posts()) :
$authors->the_post();
?>
<option value="<?php echo the_title() ?>">
<?php echo the_title() ?>
</option>
<?php
endwhile;
?>
</select>
when i select multiple options it save only one option, i want to save selected options
is there any suggestions
Saving meta values
$opt_meta_author = $_POST['opt_meta_author'];
update_post_meta( $post->ID, 'opt_meta_author', $opt_meta_author);

$opt_meta_author = unserialize(get_post_meta($post->ID, 'opt_meta_author', true));
<select name="opt_meta_author" id="opt_meta_author" multiple="multiple">
<?php
$auth_args = array(
'post_type' => 'author',
'orderby' => 'title',
'order' => 'ASC'
);
$authors = new WP_Query($auth_args);
while($authors->have_posts()) :
$authors->the_post();
?>
<option value="<?php echo the_title() ?>">
<?php echo the_title() ?>
</option>
<?php
endwhile;
?>
</select>
To get multiple selected value while storing do this:
$opt_meta_author = serialize($_POST['opt_meta_author']);

What is the var dump of $_POST['opt_meta_author'])? if it is an array, convert it in string using implode and save the string in database

Related

Wordpress Form field search via custom post types

So we currently have a drop down option that auto populates from custom post types and displays for the customer to choose from.
However, now their list is extremely long so they would like the functionality of being able to start typing a company name to reduce the amount of results in the list, but i'm completely stuck and need help converting our current option to the desired result.
<div class="custom-control__wrap">
<i class="fas fa-caret-down"></i>
<select data-field-name="customer" class="form-control validate[required] autosaveFieldDropdown" name="customer" id="customer">
<option value="" selected>Company name</option>
<?php
$args = array(
'post_type' => 'companies',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC'
);
$posts = get_posts($args);
?>
<?php foreach ($posts as $post): ?>
<option value="<?php echo $post->ID; ?>" <?php echo ($personalInfoNameObj->customer == $post->ID) ? 'selected="selected"' : '';?> ><?php echo $post->post_title ; ?></option>
<?php endforeach; ?>
</select>
</div>
Any help is greatly appreciated
Use below code it will work properly.
<div class="custom-control__wrap">
<i class="fas fa-caret-down"></i>
<select data-field-name="customer" class="form-control validate[required] autosaveFieldDropdown" name="customer" id="customer">
<option value="" selected>Company name</option>
<?php
$args = array(
'post_type' => 'companies',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC'
);
$posts = new WP_Query($args);
?>
<?php foreach ($posts as $post): ?>
<option value="<?= $post->ID; ?>" <?= ($personalInfoNameObj->customer == $post->ID) ? 'selected="selected"' : '';?> ><?= $post->post_title ; ?></option>
<?php endforeach; ?>
</select>
</div>
<?php wp_reset_postdata(); ?>
<div class="custom-control__wrap">
<?php
$query = new WP_Query(array(
'post_type' => 'companies',
'post_status' => 'publish',
'posts_per_page' => -1
));
echo "<input id=\"company\" list=\"company1\" style=\"width: 100%; height: 55px;\" placeholder=\"Start typing company name\">
<datalist id=\"company1\" >";
while ($query->have_posts()) {
$query->the_post();
$post_title = get_the_title();
$post_id = get_the_id();
echo "<option data-value=\"$post_id\" value=\"$post_title\"/>";
}
wp_reset_query();
echo "</datalist>"; ?>
</div>
Ended up using a WP Query with a while loop pushing into a datalist to get the desired result.

Keeping Select Value Wordpress Search Submit PHP

i've been at this for a couple of evening now, i've tried many different solutions and would rather keep it PHP rather than a jQuery solution or AJAX, here's my code, for some reason the Select Dropdown just won't keep it's value, any advice would be amazing. Thanks in advance.
<?php
$taxonomy = 'accommodation_area';
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => true
);
$tax_terms = get_terms( $taxonomy, $args );
?>
<select name="<? echo $taxonomy ?>" id="<? echo $taxonomy ?>" class="postform">
<option>Choose Area</option>
<?php if($tax_terms): ?>
<?php foreach ($tax_terms as $tax_term): ?>
<option value="<?php echo $tax_term->slug; ?>" <?php if ( isset( $_POST[ $taxonomy ] ) && ( $_POST[ $taxonomy ] == $tax_term->slug ) ) echo "selected";?>><?php echo $tax_term->name; ?></option>
<?php endforeach; ?>
<?php endif; ?>
</select>

Keeping the value of a select from wordpress loop

I am looking for a way to retain the selection from a a list of options which are created from within a WordPress post loop:
<?php
$args = array( 'post_type' => 'office_locations', 'posts_per_page' => -1, 'order_by' => 'title', 'order' => 'ASC' );
$loop = new WP_Query( $args ); ?>
<select style="width: 100%;" name="selectedValue" onchange="this.form.submit()">
<option disabled>Select an office location...</option> // This is disabled
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<option><?php echo get_the_title();?></option>
<?php endwhile; ?>
</select>
So if a User makes a selection I have set it to post that upon selection, is there a way for the select to remain on that option after this happens?
Sure, check the value of POST matches the value of the select, then set as selected:
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<option<?= (isset($_POST['selectedValue']) && $_POST['selectedValue'] == get_the_title() ? ' selected' : null) ?>><?php echo get_the_title();?></option>
<?php endwhile; ?>

Dropdown Wordpress authors wrong permalink | query string

I put together a couple of dropdown searches and I'm having problems with the authors.
This is the website for my archives page: http://goo.gl/p1RLUm
So if I search for an author it sends me to something like this:
mydomain.com/?author=67
When it should be sending me to this:
mydomain.com/author/username
here's an actual working author page
I've tried so many things but it always pull the query string as an answer and can never get it to go away.
Any help is appreciated!
<form action="<?php bloginfo('url'); ?>/author/" method="get">
<?php
$args = array(
'show_option_none' => __( 'Select Author' ),
'name' => 'author',
'orderby' => 'ASC',
'echo' => 0,
'who' => 'authors',
);
?>
<?php $select = wp_dropdown_users( $args ); ?>
<?php $replace = "<select$1 onchange='return this.form.submit()'>"; ?>
<?php $select = preg_replace( '#<select([^>]*)>#', $replace, $select ); ?>
<?php echo $select; ?>
<noscript>
<input type="submit" value="View" />
</noscript>
</form>
Ok so updated my code to this. Am I on the right track?
<?php $users = wp_list_authors($args); ?>
<?php
$args = array(
'show_option_none' => __( 'Select Author' ),
'name' => 'author',
'orderby' => 'ASC',
'echo' => FALSE,
'who' => 'authors',
);
?>
<select name="author-dropdown" onchange='return this.form.submit()'>
<?php foreach($users as $user):?>
<option name="<?php echo $user->name; ?>">
<?php echo $user->name; ?>
</option>
<?php endforeach;?>
</select>
Unfortunatly for you the wp_dropdown_users( $args ) function does not have an option to set the value of names. It is the ids and that's it.
What you should do is use wp_list_authors( $args ) and then do something like this :
<?php $users = wp_list_authors($args); ?>
<select name="yourname" onchange='return this.form.submit()'>
<?php foreach($users as $user):?>
<?php //just check how to access the $user name url friendly tag ?>
<option name="<?php echo $user->name; ?>"><?php echo $user->name?></option>
<?php endforeach;?>
</select>
By doing so you don't need your replace/regex thing and have more control over the users output. I'm guessing your JS does the window.location.href = redirection job.
Plus, there's an error with your redirection. You should remove the ? character in your url at submit. I guess it is also done in your JS.

get sub_field from children pages and sort them alphabetically

I'm using a php code to display children pages from a specific parent page ID to display page links in a select list type.
all my children pages have a sub_field called "artiste", with an artist name.
here is my php code to display the page name with link to the page :
<?php
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => '22',
'order' => 'ASC',
'orderby' => 'title'
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) : ?>
<div class="styled-select">
<select name="" onchange="location = this.options[this.selectedIndex].value;">
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
<option value="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></option>
<?php endwhile; ?>
</select>
</div>
<?php endif; wp_reset_query(); ?>
this works fine, I get all the children page titles and the permalink and sorted alphabetically.
now I want to get instead of the page title, a custom field from each children pages, here is my code :
my custom field is "the_sub_field('nom_artiste');"
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => '22',
'order' => 'ASC',
'orderby' => 'title'
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) : ?>
<div class="styled-select">
<select name="" onchange="location = this.options[this.selectedIndex].value;">
<option>--- A - Z ---</option>
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
<option value="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php if(get_field('artiste')): while (has_sub_field('artiste') ): ?><?php the_sub_field('nom_artiste'); ?><?php endwhile; endif; ?></option>
<?php endwhile; ?>
</select>
</div>
<?php endif; wp_reset_query(); ?>
it works also, I get the custom field instead of the page title, and the permalink of my page title, that's perfect.
but in this second code, my custom field are sorted depending of there page title.
for example, I have 2 pages :
AAAAA
ZZZZZ
and custom fields :
for AAAA : custom field is GGGG
for ZZZZ : custom field is BBBB
so with the second php code, my list is not sorted right because it sorted depending on the page title and not of custom field... I get :
GGGG
BBBB
I think I have to create an array with my custom field to be abble to sort them alphabetically before displaying them in my select onchange, but I don't know how to do it...
can anybody help me with this ?
thanks a lot for your help
Try this, replacing everything between <?php while ... endwhile; ?> in your code:
<?php
//Declare an array to hold data
$options = array();
//The loop
while ( $parent->have_posts() ) : $parent->the_post();
//Define a temporary array
$option = array();
//Get all the values we need and store them in an array.
$option['value'] = get_permalink();
$option['title'] = get_the_title();
if(get_field('artiste')):
while (has_sub_field('artiste') ) {
$option['label'] = get_sub_field('nom_artiste');
}
else:
$option['label'] = get_the_title(); //Put something here if artiste is not populated.
endif;
$options[] = $option;
endwhile;
//Sort the array on the 'label' field
usort($options, function($a, $b) { return strcmp($a['label'], $b['label']); });
//Output the <option> tags
$format = ' <option value="%s" title="%s">%s</option>'."\n";
foreach ($options as $o) {
printf($format, $o['value'], $o['title'], $o['label']);
}
?>

Categories