A nice tutorial about creating Responsive Sidebar Navigation. It's easy-to-integrate side, vertical navigation, ideal for dashboards and admin areas.
by via jQuery-Plugins.net RSS Feed
"Mr Branding" is a blog based on RSS for everything related to website branding and website design, it collects its posts from many sites in order to facilitate the updating to the latest technology.
To suggest any source, please contact me: Taha.baba@consultant.com
A nice tutorial about creating Responsive Sidebar Navigation. It's easy-to-integrate side, vertical navigation, ideal for dashboards and admin areas.
Welcome to third part of our series called "Mastering WP_Query". In the previous part, we went through 13 WordPress functions that are related to the WP_Query class. In this part, we will be reviewing WordPress hooks (filters and actions) that you can use in conjunction with WP_Query.
So, without further ado, let's begin!
In the "Filters Reference" of the Codex, there are 16 WordPress filters listed under the title "WP_Query Filters". In this section, we're going to talk about them. It will be a fairly quick section because most of the filters are about different "clauses" of a database query made by the WP_Query class.
We'll get back to them, right after going through the filters that are not related to the clauses.
found_postsThis filter allows you to change the number of found items without the limit that comes from WP_Query's posts_per_page argument.
This filter is particularly useful for creating custom pagination. Let's see what the Codex says:
For instance, if you are declaring a custom offset value in your queries, WordPress will NOT deduct the offset from the the $wp_query->found_posts parameter (for example, if you have 45 usable posts after an offset of 10, WordPress will ignore the offset and still give found_posts a value of 55).
Makes sense.
found_posts_queryThe number of found items is calculated with the SELECT FOUND_ROWS() command in MySQL. This filter lets you change this command to something else, in case you need to calculate the number of found items in a different way.
posts_requestIf you want to change the completed SQL query, this is the filter you should use. It basically overrides the whole SQL query composed by the WP_Query class.
posts_resultsIf you want to alter the PHP array that WP_Query generates with the SQL query, you can use this filter. There's a neat example over on the Codex so you can see how it works.
(Please note that this filter handles the raw array that's generated from the SQL query.)
the_postsUnlike posts_results, the the_posts filter waits for the array to be internally processed, meaning that the array would be checked against unpublished and sticky posts. Use this one if you don't want unpublished or posts in your array.
SELECT Clause) of the Query: posts_fieldsThe SELECT clause of an SQL query determines which database fields will be selected from the resulting database rows, and this filter helps you filter it.
LIMIT Clause of the Query: post_limitsThe LIMIT clause of an SQL query sets the limitations to the query, and this filter helps you filter it.
DISTINCT Clause of the Query: posts_distinctThe DISTINCT clause of an SQL query states that the query should return only different results, and this filter helps you filter it. Naturally, WP_Query does not return distinct results but when you use this filter with a function that returns "DISTINCT", the query will be adjusted to return only different results.
WHERE Clause of the Query: posts_whereThe WHERE clause of an SQL query is used to filter MySQL's SELECT, INSERT, UPDATE or DELETE statements, and this filter helps you filter this filter. While the WP_Query class does all the work needed to filter the results, you can take it a step further by using this filter.
WHERE Clause of the Query After the Paging Is Calculated: posts_where_pagedThis filter is an iteration to the posts_where filter, which you can use with paging queries.
WHERE Clause of a Search Query: posts_searchAnother iteration to the posts_where filter is this filter, which you can use to alter the WHERE clause of a query that's used to get search results in WordPress.
JOIN Clause of the Query: posts_joinThe JOIN clause of an SQL query allows you to work your SQL command within multiple database tables, and this filter helps you filter it. This is one of the advanced parts of MySQL, so I don't recommend using this filter unless you know very well how MySQL JOINs work.
JOIN Clause of the Query After the Paging Is Calculated: posts_join_pagedJust as posts_where_paged is an iteration of posts_where, this is an iteration to the posts_join filter that works on paging queries.
ORDER BY Clause of the Query: posts_orderbyThe ORDER BY clause of an SQL query arranges the ordering of the query, and this filter helps you filter the order.
GROUP BY Clause of the Query: posts_groupbyThe GROUP BY clause of an SQL query makes the query return "grouped" results by a database field, and this filter helps you filter how to group the results.
posts_clausesIf you want to deal with all clauses at the same time, there's a filter for that, too: posts_clauses. This filter covers the WHERE, GROUP BY, JOIN, ORDER BY, DISTINCT, SELECT, and LIMITS clauses.
Now we've reviewed the filters related to WP_Query, let's move on to the other kind of hooks: actions.
pre_get_postsBefore the query is parsed, you can interact with it (e.g. inject additional query variables) by using this action. Let's see a quick example from Tom McFarlin to learn how to exclude a category from the main loop:
<?php
function tutsplus_exclude_category( $wp_query ) {
/*
* Add the category to an array of excluded categories. In this case, though,
* it's really just one.
*/
$excluded = array( '-1' );
/*
* Note that this is a different, perhaps, cleaner way to write:
*
* $wp_query->set( 'category__not_in', $excluded );
*/
set_query_var( 'category__not_in', $excluded );
}
add_action( 'pre_get_posts', 'tutsplus_exclude_category' );
?>
parse_queryUnlike pre_get_posts, which intervenes with the query before the query variables are set, the parse_query action handles the process after the query variables are set. Thus, you should use this action if you want to check the current variables and take action in accordance with them.
the_postThe term the_action is a little bit confusing because it's the name of an action hook, a WP_Query-related function and also a method of the WP_Query class.
This action, as the Codex says, lets us modify the post object immediately after being queried and set up. Using this action, you can change the output directly. Let's see a quick example:
<?php
function tutsplus_featured_badge( $post ) {
if ( is_single() && in_category( 'featured' ) ) {
echo '<div class="featured-badge">' . __( 'FEATURED', 'tutsplus' ) . '</div>';
}
}
add_action( 'the_post', 'tutsplus_featured_badge' );
?>
Actions and filters are always a fun topic to talk and write about. (I know for a fact that my two series about WordPress actions and WordPress filters were fun to write and got a great reaction from our reader base.) I hope you enjoyed this part as much as I enjoyed writing it.
Want to add anything to the article? Shoot a comment in the comments section below. And if you liked the article, don't forget to share it with your friends!
See you in the next part, where we'll be talking about the properties and methods of the WP_Query class!
Have you tried Blab.im? Wondering how to get the most out of it? Blab, a live-casting video platform, has many great features for connecting with and growing your audience. In this post you’ll discover 15 tips to help marketers succeed with Blab. Joining Blab Are you new to Blab? If so, joining is a breeze. You […]
This post 15 Blab Tips for Marketers: Ultimate Guide to Blab.im first appeared on Social Media Examiner.
Social Media Examiner - Your Guide to the Social Media Jungle
Do you use Facebook advertising? Have you considered incorporating customer reviews into your ads? Facebook ads that include customer reviews blend in with other news feed stories and are better received by people browsing Facebook. In this article I’ll share how to improve your Facebook ads with customer reviews. #1: Choose the Right Review In social advertising it’s vital […]
This post How to Improve Your Facebook Ads With Customer Reviews first appeared on Social Media Examiner.
Social Media Examiner - Your Guide to the Social Media Jungle