Keeping the value of a select from wordpress loop - php

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; ?>

Related

Wordpress pagination with custom posts_per_page issue

In a custom Woocommerce products page I filter posts_per_page with a dropdown menu. It works well and the table is updated with the new value.
But then if I click on the 'Next' button, next_posts_link doesn't post the new posts_per_page value, and the second page starts from record #11, as default.
Any solution?
<?php
$prodNum = $_POST['num_prods'];
?>
<!-- SELECT POST PER PAGES -->
<form id="numProdsForm" method="POST">
<select name="num_prods" id="num_prods" onchange="this.form.submit()">
<option value="10" <?php if ($prodNum==10){ ?> selected <?php } ?>>10</option>
<option value="20" <?php if ($prodNum==20){ ?> selected <?php } ?>>20</option>
<option value="30" <?php if ($prodNum==30){ ?> selected <?php } ?>>30</option>
<option value="50" <?php if ($prodNum==40){ ?> selected <?php } ?>>50</option>
<option value="100" <?php if ($prodNum==50){ ?> selected <?php } ?>>100</option>
</select>
</form>
<!-- CUSTOM TABLE -->
<table>
<?php if ( woocommerce_product_loop() ) { ?>
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => $prodNum,
'orderby'=>'title',
'order' => 'ASC'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
?>
<tr>
<td class="flex-row" role="cell"><?php echo $product->get_attribute( 'color' ); ?></td>
</tr>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<?php
} else {
do_action( 'woocommerce_no_products_found' );
}
?>
</table>
<div class="products-pagination">
<?php previous_posts_link( '« PREV', $loop->max_num_pages) ?>
<?php next_posts_link( 'NEXT »', $loop->max_num_pages) ?>
</div>
I've tried to put the pagination before wp_reset_query();, or to use get $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; but nothing changes.
You could try using a "GET" rather than "POST" for the form, and pass the offset and number of posts to show as URL params. Then you can add the offset attribute to the query. You can also optimize some of your code by using selected selected function definition rather than your if statements for the options.
<?php
$product_number = ( isset( $_GET['num_prods'] ) ) ? absint( $_GET['num_prods'] ) : 0;
$existing_offset = ( isset( $_GET['offset'] ) ) ? absint( $_GET['offset'] ) : 0;
$offset = $product_number + $existing_offset;
?>
<!-- SELECT POST PER PAGES -->
<form id="numProdsForm" method="GET">
<input type="hidden" name="offset" value="<?php echo esc_attr( $offset ); ?>">
<select name="num_prods" id="num_prods" onchange="this.form.submit()">
<option value=""> - - Choose Number of Products to Show -- </option>
<option value="10" <?php selected( 10, $product_number ); ?>>10</option>
<option value="20" <?php selected( 20, $product_number ); ?>>20</option>
<option value="30" <?php selected( 30, $product_number ); ?>>30</option>
<option value="50" <?php selected( 50, $product_number ); ?>>50</option>
<option value="100" <?php selected( 100, $product_number ); ?>>100</option>
</select>
</form>
<!-- CUSTOM TABLE -->
<table>
<?php if ( woocommerce_product_loop() ) { ?>
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => $product_number,
'offset' => $offset, // the offset.
'orderby' => 'title',
'order' => 'ASC',
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) :
$loop->the_post();
global $product;
?>
<tr>
<td class="flex-row" role="cell"><?php echo $product->get_attribute( 'color' ); ?></td>
</tr>
<?php endwhile; ?>
<?php wp_reset_postdata(); // use wp_reset_postdata(). ?>
<?php
} else {
do_action( 'woocommerce_no_products_found' );
}
?>
</table>
<div class="products-pagination">
<?php previous_posts_link( '« PREV', $loop->max_num_pages ); ?>
<?php next_posts_link( 'NEXT »', $loop->max_num_pages ); ?>
</div>

sort by dropdown queries custom post type

Wanted to have a feature of a dropdown on my custom archive template like this https://facetwp.com/demo/cars/?_sort=title_asc
here is my code but doesnt sort any of the custom post either by title or for ratings.
<?php
$order = "&order=ASC";
if ($_POST['select'] == 'rmp_get_avg_rating') { $order =
"&order=ASC&orderby=ratings"; }
if ($_POST['select'] == 'title') { $order =
"&order=ASC&orderby=title"; }
?>
<form method="post" id="order">
Sort by:
<select name="select" onchange='this.form.submit()'>
<option value="ratings"<?php selected(
$_POST['select'],'rmp_get_avg_rating', 1 ); ?>>Highest Rated</option>
<option value="alphabetical"<?php selected(
$_POST['select'],'alphabetical' , 1 ); ?>>Alphabetical</option>
</select>
</form>
<?php $loop = new WP_Query(
array(
'post_type' => 'company',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1
)
);
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
Ok I managed to resolved this
here is the working code
<?php
$loop = isset($_GET['orderby']) ? sanitize_text_field($_GET['orderby']) :
"";
if(!empty($loop) && "title" == $loop):
$loop = new WP_Query(array(
'post_type'=>'company',
'posts_per_page'=>-1,
'orderby'=> "title",
'order'=>'ASC',
));
endif;
if(!empty($loop) && "rmp_get_avg_rating" == $loop):
$loop = new WP_Query(array(
'post_type'=>'company',
'posts_per_page'=>-1,
'orderby'=> "rmp_get_avg_rating",
'order'=>'ASC',
));
endif;
?>
<form method="GET">
<select name="orderby" id="orderby">
<option value="select">Select</option>
<option value="rmp_get_avg_rating">Highest Rated</option>
<option value="title">Alphabetical</option>
</select>
<button type="submit">Apply</button>
</form>
</div>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

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>

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']);
}
?>

Multiple Select value saving Issue

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

Categories