I have a page that displays all the staff members, which are grouped by company. Each staff member is a custom post type. I have a dropdown menu and a submit button to filter the staff by company, which works fine.
<div id="choose">
<form name="selectCompany" method="post" action="">
<span>Select Company: </span>
<select name="company" >
<option value="All Staff">All Staff</option>
<option value="Company 1" <?php echo $yourgroup; ?>>Company 1</option>
<option value="Company 2" <?php echo $yourpower; ?>>Company 2</option>
<option value="Company 3" <?php echo $yourelectrical; ?>>Company 3/option>
<option value="Company 4" <?php echo $newmills; ?>>Company 4"</option>
</select>
<input type="submit" value="Go" name="submit" />
</form>
</div>
The Wordpress query I'm using is (where $company is the value from the dropdown menu):
if ($company != "All Staff") {
$mypost = array( 'post_type' => 'staff_members',
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'company',
'value' => $company,
'compare' => '=',
),
),
);
}
else {
$mypost = array( 'post_type' => 'staff_members',
'orderby' => 'title',
'order' => 'ASC',);
}
if (empty($_POST['submit'])) { //Set default value for displaying staff
$mypost = array( 'post_type' => 'staff_members',
'orderby' => 'title',
'order' => 'ASC',);
$company = "All Staff";
}
?>
<p><span class="display">Displaying Staff for <?php echo $company; ?></span></p>
<?php
$loop = new WP_Query( $mypost );
while ( $loop->have_posts() ) : $loop->the_post();?>
<div class="staff">
<div class="photo">
<a href="<?php the_permalink(); ?>" rel="shadowbox[staff-1]">
<?php
if ( '' != get_the_post_thumbnail() ) { //Recommended way of doing it
the_post_thumbnail();
}
else {
echo '<img src="' . get_bloginfo( 'stylesheet_directory' ) . '/staff-default.gif" />';
}
?>
</a>
</div>
<div class="name"> <?php the_title(); ?></div>
<div class="title">
<?php echo esc_html( get_post_meta( get_the_ID(), 'job_title', true ) ); ?>
</div>
<?php // echo esc_html( get_post_meta( get_the_ID(), 'company', true ) ); ?>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<?php get_footer(); ?>
This all works fine, however I want to remove the submit button and just do it when the list changes. I changed it to <select name="company" onchange="this.form.submit()">. It submits the form and the page reloads but it doesn't actually filter the staff. I don't know why using a submit button works but but using onchange doesn't.
Related
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.
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>
I have a repair prices calculator on my website, where users can first select the brand from a dropdown and then select the exact model from the second dropdown.
Currently the page reloads after selecting an option from the first dropdown and passes the selected value with $_POST, the second dropdown then shows all devices from the selected brand. After selecting the model, the user gets forwarded to the specific device page.
I would prefer to pass the value and show the second dropdown without the page being reloaded. I have tried doing this with ajax, but wasn't able to get the correct code, so I went back to the first working setup. Could anyone tell me the code I need to submit the value to the second dropdown without reloading?
This is the code I have so far:
<h2>Preisrechner</h2>
<div class="preisrechner-select-wrapper">
<form method="POST" action="">
<div>
<?php
$args = array(
'hierarchical' => 1,
'depth' => 1,
'orderby' => 'name',
'echo' => 0,
'taxonomy' => 'marke',
// this leads to variable name $_POST['marke']
'name' => 'marke-sel'
);
if( ! isset($_POST['marke-sel']) ):
$args['show_option_none'] = 'Hersteller auswählen';
else:
$args['selected'] = $_POST['marke-sel'];
endif;
$marke = wp_dropdown_categories( $args );
// this enables the buttonless js possibility
$marke = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $marke);
echo $marke;
?>
<noscript>
<div>
<input type="submit" value="marke"/>
</div>
</noscript>
</div>
</form>
<?php
if( isset($_POST['marke-sel']) && $_POST['marke-sel'] ):
?>
<form method="POST" action="<? bloginfo('url'); ?>">
<input type="hidden" name="marke" value="<?php echo $_POST['marke-sel'] ?>">
<div>
<?php
$the_query = new WP_Query( array(
'post_type' => 'reparaturpreise',
'tax_query' => array(
array (
'taxonomy' => 'marke',
'field' => 'id',
'terms' => $_POST['marke-sel'],
)
),
) );
if ( $the_query->have_posts() ) :
?>
<div class="form-option parent-field-wrapper">
<label for=""/>
<select name='modell' id='modell' onchange='document.location=this.value'>
<option value="">Modell auswählen</option>
<?php while ( $the_query->have_posts() ) :
$the_query->the_post();
?>
<option value="<?php the_permalink();?>">
<?php the_title(); ?>
</option>
<?php endwhile; ?>
</select>
</div>
<?php endif;
wp_reset_postdata();
$modell = preg_replace("#<select([^>]*)>#", "<select$1 onchange='this.form.submit()'>", $modell);
echo $modell;
?>
<noscript>
<div>
<input type="submit" value="modell"/>
</div>
</noscript>
</div>
</form>
<?php endif; ?>
<?php
if( isset($_POST['marke-sel']) && $_POST['modell'] ):
$args = array(
'post_type' => 'reparaturpreise',
'cat' => $_POST['marke-sel'],
'posts_per_page' => 1
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
endif;
?>
</div>
If the code generated by PHP needs to change (ie results) then the page will need to reload. The only way around this would be to move this code to an endpoint which can be called via an AJAX call and returns the results.
A good example which can be changed for your scenario can be found here:
https://premium.wpmudev.org/blog/using-ajax-with-wordpress/
I need that this select worked here I have this code need a wordpress filter
I have a page of posts that needs to be filtered out by date I can not do that to work properly
<?php
if ($_GET['select'] == '2018') {
$order = "&orderby=date&order=DESC";
$s1 = ' selected="selected"';
}
if ($_GET['select'] == 'lastest') {
$order = "&orderby=date&order=ASC";
$s2 = ' selected="selected"';
}
if ($_GET['select'] == 'title') {
$order = "&orderby=title&order=ASC";
$s3 = ' selected="selected"';
}
if ($_GET['select'] == 'correct') {
$order = "&orderby=modified";
$s4 = ' selected="selected"';
}
?>
<form method="get" id="order">
<select name="select" onchange='this.form.submit()' style="width:200px">
<option value="2018"<?=$s1?>>2018</option>
<option value="lastest"<?=$s2?>>по дате (сначала старые)</option>
<option value="title"<?=$s3?>>по заголовку</option>
<option value="correct"<?=$s4?>>по дате изменения</option>
</select>
</form>
<?php
global $query_string; // параметры базового запроса
query_posts($query_string.'&'.$order); // базовый запрос + свои параметры
?>
<div class="news__page" id="container-01">
<?php
$args2 = array(
'category_name' => 'news-company',
'date_query' => array(
array(
'before' => array(
'year' => 2002,
),
'inclusive' => true,
),
),
'posts_per_page' => -1,
);
$loop = new WP_Query($args2 );
while ($loop->have_posts()) {
$loop->the_post();
?>
<div class="news-item pinto">
<div class="news-img">
<?php if ( has_post_thumbnail() ) : ?>
<a title="<?php the_title_attribute(); ?>" href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
</a>
<?php endif; ?>
</div>
<div class="news-wrap">
<?php the_title(); ?>
<div class="news_text"><?php the_excerpt(); ?></div>
<span class="news-data"><?php echo get_the_date() ?></span>
</div>
</div>
<?php
}
?>
I need that this select worked here I have this code need a wordpress filter
I have a page of posts that needs to be filtered out by date I can not do that to work properly
http://prntscr.com/ksbdia
I use this <?php echo $product->get_stock_quantity(); ?> this to get woo-commerce product quantity post loop and its works, but when I use this in function.php in a function loop then its not work. this return a php fatal error.
Error like this: PHP Fatal error: Call to a member function get_stock_quantity() on null in /home/username/public_html/wp-content/themes/etrostore-child-theme/load-more-posts.php on line 142
So the <?php echo $product->get_stock_quantity(); ?> code works in page template. But not work in function.php
Here is my function.php code:
function filter_post_ajax(){
global $product;
$noffset = $_POST["noffset"];
$nppp = $_POST["nppp"];
$cat_id = $_POST[ 'category' ];
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
//'cat' => $cat_id,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $cat_id,
'operator' => 'IN'
)),
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => $nppp,
'offset' => $noffset,
);
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post(); ?>
<div class="col-xs-6 col-sm-4 col-md-3 product-cols">
<div class="product-image">
<a href="<?php the_permalink()?>" >
<?php the_post_thumbnail( 'medium' ); ?>
</a>
<div class="overlay">
<div class="overly_cart_details animated fadeIn">
Add to Bag
</div>
</div>
</div>
<div class="product-title">
<h3><?php the_title(); ?></h3>
</div>
<?php
$saleprice = get_post_meta( get_the_ID(), '_sale_price', true);
$regularprice = get_post_meta( get_the_ID(), '_regular_price', true);
?>
<?php if(!(empty($regularprice)) ){ ?>
<div class="product-price">
<span class="price">
<?php if ( $saleprice ) { ?>
৳ <?php echo BanglaConverter::en2bn($saleprice); ?>
<?php } else { ?>
৳ <?php echo BanglaConverter::en2bn($regularprice); ?>
<?php } ?>
</span>
</div>
<div class="product-add-bag add-to-cart-btn">
<button class="plusminus">+</button>
<div class="quantites">
<input type="number" class="input-text qty text" step="1" min="1" max="<?php echo $product->get_stock_quantity(); ?>" name="quantity" value="<?php if(woo_in_cart($product->id)) {echo woo_in_cart($product->id);}else{echo '1';} ?>" title="Qty" size="4" pattern="[0-9]*" inputmode="numeric">
</div>
<button class="plusminus">-</button>
<?php woocommerce_template_loop_add_to_cart(); ?>
</div>
<?php } else {?>
<div class="product-add-bag add-to-cart-btn">
View Details
</div>
<?php } ?>
</div> <!-- col-md-4 -->
<?php endwhile; die();
}
What's wrong I'm doing here? Please help me.
You have declared global $product outside the loop, so it is not taking $product as an object.
while ($loop->have_posts()) : $loop->the_post();
$p=wc_get_product(get_the_ID()); //it will return the current product object
echo $p->get_stock_quantity();
/* your other codes */
endwhile;
hope this will help you...