how can i change WP main archives loop to sort by name or title

Use pre_get_posts to do any alterations of the main query. The main query happens before the template is loaded, using query_posts just overwrites that original query, which is a waste of resources: function wpa82795_archives_orderby( $query ) { if ( $query->is_archive() && $query->is_main_query() ) { $query->set( ‘orderby’, ‘title’ ); $query->set( ‘order’, ‘asc’ ); } } add_action( … Read more

How to get date of post when using wp_get_recent_posts()?

You can see everything returned via php’s var_dump or print_r: $recent_posts = wp_get_recent_posts(); echo ‘<pre>’; print_r( $recent_posts ); echo ‘</pre>’; This will reveal that the date is stored in post_date: foreach( $recent_posts as $recent ): echo $recent[‘post_date’]; endforeach; Or to format the date, use php’s date and strtotime: echo date( ‘l F jS’, strtotime( $recent[‘post_date’] … Read more

How to order posts in an arbitrary order?

First off, query_posts is terrible doesn’t use it. It does all kinds of fun things like mess up the very useful conditional tags like is_singular and the like as well as mess up pagination. Short of hooking into the posts_orderby filter and writing some custom order by SQL, your best bet is to use the … Read more

Order posts alphabetically: how to set order=asc in mysql query?

Why don’t you use WordPress get_posts? You can add the parameter orderby => ‘title’ as you want. $args = array( ‘posts_per_page’ => -1, ‘order’ => ‘ASC’, ‘orderby’ => ‘title’ ); $posts = get_posts( $args ); print_r($posts); If you want the sql you can just add ORDER BY post_title: SELECT * FROM $wpdb->posts WHERE post_type=”post” AND … Read more

Single.php – Get Current Parent Category

The very broad answer for why this doesn’t work is that the internet is stateless. Basically each request for a page is a separate and unique instance from other page requests. Example Let’s say post-1 is in category-1 and category-2. When WordPress loads post-1 on single.php it has no way of knowing how the user … Read more

Display post category in foreach loop

When the foreach loop is from get_posts(), you need to run setup_postdata() for each post if you want to use themplate tags inside the loop like the_category(), if not you can get unexpected behaviour. Also, you will need to run wp_reset_postdata() after the foreach loop. Note that the_category and similar template tags actually displays/echoes the … Read more

An array of latest wp_titles

You can use the WordPress function wp_get_recent_posts(), with required args. $output = array(); $args = array( ‘numberposts’ => 10, ‘offset’ => 0, ‘category’ => 0, ‘orderby’ => ‘post_date’, ‘order’ => ‘DESC’, ‘include’ => , ‘exclude’ => , ‘meta_key’ => , ‘meta_value’ =>, ‘post_type’ => ‘post’, ‘post_status’ => ‘draft, publish, future, pending, private’, ‘suppress_filters’ => true … Read more

Only display sticky post or latest post in custom loop

You can adapt your first loop to either show a sticky post or the latest post in the following manner: PLEASE NOTE: You have to reset your custom query <?php $args = array( ‘posts_per_page’ => 1, ‘post__in’ => get_option( ‘sticky_posts’ ), ‘ignore_sticky_posts’ => 1 ); $my_query = new WP_Query( $args ); $do_not_duplicate = array(); while … Read more