Filtering articles and adding the date to the title tag
Some more useful code snippets for site readers.
Adding the date to the title tag of your articles on the site.
Sometimes, in order to show the age of an article for search engines, they use the date in the title tag.. The date in this tag is metadata for search engines.. I don't do it myself, but I think, SEO specialists can evaluate this code. In my opinion, this may be useful for news resources.
1 2 3 4 5 |
add_filter( 'wp_title', 'append_to_title', 10, 3 ); function append_to_title($title) { $date = get_the_date(); return $title .= " | ".$date; } |
Another just bomb code, honestly found in bynet )
I think a lot of people have this problem, when the site starts to grow and there are a lot of custom fields on it, in which it is difficult to find the necessary information. For example, it can be an online store with fields containing the type of product by which all these products should be filtered.. I recently found a solution to this problem, how to make a drop down list to filter by all custom fields. The list will appear at the top above the list of articles in the admin panel.
To solve this filtering task of this type, add the code to the file function.php and happy with the result. Code below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
add_filter( 'parse_query', 'ba_admin_posts_filter' ); add_action( 'restrict_manage_posts', 'ba_admin_posts_filter_restrict_manage_posts' ); function ba_admin_posts_filter( $query ) { global $pagenow; if ( is_admin() && $pagenow=='edit.php' && isset($_GET['ADMIN_FILTER_FIELD_NAME']) && $_GET['ADMIN_FILTER_FIELD_NAME'] != '') { $query->query_vars['meta_key'] = $_GET['ADMIN_FILTER_FIELD_NAME']; if (isset($_GET['ADMIN_FILTER_FIELD_VALUE']) && $_GET['ADMIN_FILTER_FIELD_VALUE'] != '') $query->query_vars['meta_value'] = $_GET['ADMIN_FILTER_FIELD_VALUE']; } } function ba_admin_posts_filter_restrict_manage_posts() { global $wpdb; $sql = 'SELECT DISTINCT meta_key FROM '.$wpdb->postmeta.' ORDER BY 1'; $fields = $wpdb->get_results($sql, ARRAY_N); ?> <select name="ADMIN_FILTER_FIELD_NAME"> <option value=""><?php _e('Filter By Custom Fields', 'baapf'); ?></option> <?php $current = isset($_GET['ADMIN_FILTER_FIELD_NAME'])? $_GET['ADMIN_FILTER_FIELD_NAME']:''; $current_v = isset($_GET['ADMIN_FILTER_FIELD_VALUE'])? $_GET['ADMIN_FILTER_FIELD_VALUE']:''; foreach ($fields as $field) { if (substr($field[0],0,1) != "_"){ printf ( '<option value="%s"%s>%s</option>', $field[0], $field[0] == $current? ' selected="selected"':'', $field[0] ); } } ?> </select> <?php _e('Value:', 'baapf'); ?><input type="TEXT" name="ADMIN_FILTER_FIELD_VALUE" value="<?php echo $current_v; ?>" /> <?php } |
You can see the script in action in the screenshot.:
Code works great, checked everything on the test site.
/*
- Basic web design course;
- Site layout;
- General course on CMS WordPress and continuation of the course on template development;
- Website development in PHP.
Thanks for the great addition.. Protestil code, generally works, sorts by all custom fields, except one, which is exactly what I needed.
All my records have a custom field instock with the value 1 or 0. Sort with value 1 works fine, but when trying to sort the records with the value 0 does not work, the filter just stupidly displays all posts of the category, and with meaning 0 and 1.
It looks like the code does not want to accept the value 0 custom field instock. How to teach it to sort by value as well 0?
I have an old version, 2.7.1, update is not yet possible due to incompatibility with a complex online store plugin.
Thanks.
I'll try to watch tonight
It really pleases. And that, just checked, filter works with value 00, only 0 does not perceive.
Thanks again.
Just added on the test site 3 records with a new custom field. For two made a difference 0, but for one 1. As a result, it turned out to filter out only by zero value, as a result received 2 records and by value “1” got one too. Maybe you're doing something wrong, Or maybe I didn't understand the question.?
Apparently there is something wrong with my wordpress installation., the question is understood correctly, I still can't sort by value 0.
I think to do something.
Try to make another custom field with the same parameters, to contain 0 and 1, and try to filter by it. If it does not work out, maybe it's a problem with wordpress. You can try to update the CMS on the test site, the main thing is that a copy of the database and files be )
Thanks for the good idea, tried to set the value 0 another field and sort by 0 works fine. So the problem is not the meaning 0 and not in WordPress, but in this particular instock field, it is in this field that it is impossible to sort by value 0.
Glad it worked out, if so write how to solve the problem completely.