get_posts() and global variables

get_posts doesn’t modify global variables & is safe to use anywhere but setup_postdata does modify the global variables. Use the function wp_reset_postdata() just after the foreach loop. It reverses the changes made by setup_postdata. The thing it does is basically same as you did in your example, you just won’t need to worry about the … Read more

WP Query – posts only by specific user

If all you need to do is add that extra parameter, just concatenate it on to the end of $args. (As a side note: I recommend using an array for args instead of the query string method as I find it much more readable.) <?php …. $args=”&posts_per_page=”.$numreviews.’_reviews&order=DESC&orderby=’.$feedsort.$metakey.’&author=123′; ….

Using WP_Query() in Magento shows error

__() is deprecated in magento. You may either manually delete it from the magento core files (& repeat this process again everytime you have a new install or you upgrade) or wait for magento developers to remove this. For wordpress, in the future when php 5.3 is much more common & wordpress breaks their backward … Read more

How to Order a list of taxonomies? orderby?

If you use the more generic get_terms you can include in the $args field all of the information to get the term or the children and specifically say what you want to order by and how you want it to be ordered. Ex. $termchildren = get_terms( “location”, array( ‘child_of’ => 4, ‘orderby’ => ‘name’, ‘order’ … Read more

How to check the array values, what WP_Query has brought to me?

Assuming you start with: $custom_query_args = array( ‘cat’ => 10, ‘post_per_page’ => 5 ); $custom_query = new WP_Query($custom_query_args); Then to answer your first question, you can see what is returned (for development/debugging) via: var_dump( $custom_query ); To output your query as a normal loop, you simply need to call a custom loop, using the have_posts() … Read more

Default permalink structure causing Notice: Undefined property: WP_Query::$post

Finally I found what causing this problem, turns out it have to do with my pre_get_post hook. Because I using is_page to check the specific page, which is not appropriate. Here is what I did function check_page($wp_query){ if($wp_query->is_page(array(‘1′,’2′,’3’)) ){ //do something here } return $wp_query; } add_action( ‘pre_get_posts’, ‘check_page’ ); So my solution to this … Read more

Specify strict ‘order by’ in WordPress query

You could use usort(). Your callback function would need to determine the last names and sort by them. So, for instance: $args = array( ‘post_type’ = ‘Employees’, ); $employees = get_posts( $args ); usort( $employees, ‘wp91501_sort_employees’ ); and then your callback might be: function wp91501_sort_employees( $a, $b ) { // split the names into arrays … Read more