I want to enable website visitors to order WordPress posts on the category page by date posted or by search keyword, similarly to how it's done on this page:
http://www.steinwaymusical.com/news.php
I'd appreciate a plugin recommendation or any other suggestion from knowledgeable people.
Thank you in advance!
WordPress has order and orderby options you can use in the query.
https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
<?php
$args = array('order' => 'ASC', 'orderby' => 'name');
$query = new WP_Query($args);
while ( $query->have_posts() ) : $query->the_post();
// echo out the title, excerpt
endwhile;
?>
Your example page uses GET variables.
?selSort=name_asc&txtKeyword=sdfsdf
So you need to create a form with method="GET" that submit GET data to the current page. Then, using PHP, you can check if any GET data is set (in this case, selSort and txtKeyword). If either of those are set, put them into your query. Then you can modify the query to resemble this:
<?php
$args = array('order' => $_GET['selSort'], 'orderby' => $_GET['txtKeyword']);
$query = new WP_Query($args);
while ( $query->have_posts() ) : $query->the_post();
// echo out the title, excerpt
endwhile;
?>
Related
I am using Yii2 with Pjax for index/gridview listing. using pjax pagination , search all working fine without postback to server.
My problem starts now,
suppose i am on page number 2, i have clicked on edit record of that 2nd page list, i reach to update view, i have done changes and saved, now i am redirected to view , now i clicked on index link from breadcrumbs.
i want to reach to page number 2 of index rather then 1st page.
Traditional process for this is get refereeing page params an append that in breadcrumbs.
But is there any simple approach to this problem where i can write few lines of code and its applied to every where in backend?
Thanks for reading.
For remembering grid filter, pages i use yii2-grid-view-state
If you need to store page only, isn't it quite easy to pass page param into your view url (<model/view>) like <model>/view?id=<id>&page=<page>?
in your index.php view, edit your ActionColumn as follow:
[
'class' => 'yii\grid\ActionColumn',
'urlCreator' => function ($action, $model, $key, $index) {
return \yii\helpers\Url::to([$action, 'id' => $model->id, 'page' => Yii::$app->request->getQueryParam('page', null)]);
},
],
As you can see, I'm getting page param from request url and pass it to models' action buttons (to all buttons, but in your question it would be enough for view button of course)
And when you click to view model, in our Controller we need to get that page value and pass it to our view.php view (in order to place it in breadcrumbs).
Our ModelController:
public function actionView($id, $page = null)
{
return $this->render('view', [
'model' => $this->findModel($id),
'page' => $page,
]);
}
And finally view.php view will get the page value, and populate the index url (if not null):
/* #var $page int */
$this->title = $model->name;
$this->params['breadcrumbs'][] = ['label' => 'Index', 'url' => ['index', 'page' => $page]];
So when you press the Index breadcrumb, it will open the page, where you entered from.
Some advantages againts session implementation (#user1764431 solution):
Each of your tab can return to it's own last page
Simple and stupid solution
Some disadvantages:
If you need to store some filter params, url could stretch very long
Just add following Code in every controller of actionIndex() rest all things will take care
$searchModel = new CentervideosSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
/*Code insertion block begin*/
$params = Yii::$app->request->queryParams;
if (count($params) <= 1)
{
$params = Yii::$app->session['customerparams'];
if(isset(Yii::$app->session['customerparams']['page']))
$_GET['page'] = Yii::$app->session['customerparams']['page'];
if(isset(Yii::$app->session['customerparams']['per-page']))
$_GET['per-page'] = Yii::$app->session['customerparams']['per-page'];
}
else
{
Yii::$app->session['customerparams'] = $params;
}
$dataProvider = $searchModel->search($params);
/*Code insertion block Ends*/
I want to add a product's size attribute to the product information shown by the Woocommerce [product] shortcode.
Is there a way to do this? I don't think it is a standard parameter to the shortcode so am assuming the code will need amending?
You could either unregister and re-register your own version of the shortcode OR the [product] shortcode displays content via
<?php wc_get_template_part( 'content', 'product' ); ?>
Therefore you can customize the the content-product.php template in your own theme via template overrides or via hooks/filters. (The content-product.php template is mostly just a few action hooks so it looks a little sparse if you aren't familiar with hooks.)
I would probably go the hooks route. Assuming your attribute is called "pa_size" (WooCommerce always prefaces the attribute with "pa_" so it know that the taxonomy is a "product attribute") I would add the following to my theme's functions.php
function kia_add_size_attr(){
global $product;
$attribute_name = "pa_size";
$results = wc_get_product_terms( $product->id, $attribute_name, array('fields' => 'names') );
if ( $results ) {
echo '<label>' . wc_attribute_label($attribute_name) . ': </label>' . implode( ', ', $results );
}
}
add_action('woocommerce_after_shop_loop_item_title', 'kia_add_size_attr' );
Do keep in mind that this will add the attribute to every "loop" product with a size attribute. If you only want to apply it to the shortcode, then you probably need to write your own shortcode.
I'm having an issue with pagination on a static posts page. Under Reading > Settings, "Posts page" is set to a static page. "Blog pages show at most" is set to 10 posts. The first ten posts show up fine, but then the pagination doesn't work (returns a 404 for "Previous posts"-link).
I've searched for an answer to this question, it seems to be fairly common and it's even mentioned in the WP Codex (http://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters), but all the solutions involving modyfying the default query haven't been working for me, e.g:
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$args = array('post_type'=>'post','posts_per_page'=>10,'paged'=>$paged);
query_posts($args);
OR
function qm_blog_pagination( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$query->set( 'paged', $paged );
}
}
add_action( 'pre_get_posts', 'qm_blog_pagination' );
Any help would be greatly appreciated.
Thank you,
Pim
In Woocommerce you can add global product attributes and terms. So for instance:
Size (attribute)
small (term)
medium (term)
large (term)
This is product independent. You can then select from the pre defined attributes on a product.
I need to get all of the terms in an attribute with php. So select the attribute required, eg size, and then return an array including [small,medium,large].
Seems simple enough but I can't find any help on doing this.
Slightly confusing, especially when looking through the WooCommerce Docs since there is absolutely no mention of getting a list of the terms/attributes.
The Attributes are saved as a custom taxonomy, and the terms are taxonomy terms. That means you can use native Wordpress functions: Wordpress get_terms() Function Reference
By clicking on an attribute in WooCommerce, you can look in the URL and you can see they are all prepended with 'pa_'
This is likely what you need:
$terms = get_terms("pa_size");
foreach ( $terms as $term ) {
echo "<option>" . $term->name . "</option>";
}
I wanted to be able to get all the different attributes from the backend that were set, and get them in an array for me to work with, I took some code from the class-wc-admin-attributes.php file and modified it for my needs:
<?php
$attribute_taxonomies = wc_get_attribute_taxonomies();
$taxonomy_terms = array();
if ($attribute_taxonomies) :
foreach ($attribute_taxonomies as $tax) :
if (taxonomy_exists(wc_attribute_taxonomy_name($tax->attribute_name))) :
$taxonomy_terms[$tax->attribute_name] = get_terms(wc_attribute_taxonomy_name($tax->attribute_name), 'orderby=name&hide_empty=0');
endif;
endforeach;
endif;
var_dump($taxonomy_terms);
exit;
This will loop through all the attribute taxonomies, retrieve the terms for each, leaving you with an array of term objects to work with for each taxonomy.
Since 4.5.0, taxonomies should be passed via the ‘taxonomy’ argument in the $args array:
$terms = get_terms( array(
'taxonomy' => 'pa_taxonyname',
'hide_empty' => false,
) );
For example, if the taxonomy slug is 'date', so the taxonomy will be 'pa_date'.
You can also mouse over the attribute name and see the taxonomy name at the bottom of the browser.
I hope it helps!
I use this:
echo '<h1>variations</h1>';
mario( $product->get_available_variations());
echo '<h1>Atributos</h1>';
mario($product->get_attributes());
echo '<h1>Poste Terms</h1>';
mario(wp_get_post_terms( $post->ID, 'pa_color'));
function mario($texto){
echo '<pre>';var_dump($texto);echo '</pre>';
};
Really with: "wp_get_post_terms( $post->ID, 'pa_color')" i search only one term, but the idea is to loop for the key ['name'] that return that function.
Get all attributes with attribute terms in woocommerce.
$attributes = wc_get_attribute_taxonomies();
foreach ($attributes as $attribute) {
$attribute->attribute_terms = get_terms(array(
'taxonomy' => 'pa_'.$attribute->attribute_name,
'hide_empty' => false,
));
}
i am trying to retrieve some simple data from the database, and i don't know where i am wrong.
i want to get all the inactive users from my database. For that, i have in my controller:
public function action_useremails()
{
$users = Model::factory('user')->where('user_status', '=', 2);
$this->view->users = $users;
}
and in the view:
<? foreach ($users as $user): ?>
<tr>
<td><span><?= $user; ?></span></td>
</tr>
<? endforeach; ?>
butm even though i have inactive users, i don't see anything in the view. I wonder where am i wrong?
}
You're building the query, but not executing it. Try:
$this->view->users = $users->execute();
also consider that the $users variable in the view will be an array of arrays, you'll have to echo the right element.
If you are talking about Kohana 2, your query must be:
$users = Model::factory('user')->where('user_status', 2)->find();
Note the args in where() block & the find() at last. Without find(), you are just building the query, but not running it.
Docs: http://docs.kohanaphp.com/libraries/orm#find
If it is KO3, it has to be similar to the above.
for v3.0 add the find_all() method at the end:
$users = Model::factory('user')->where('user_status', '=', 2)->find_all();
It executes the query you built before with the other methods.