Change post order by meta key, per post basis

Don’t try to make WP_Query, or SQL, do this. In the backend, don’t save artist_last_name save something like artist_sort_name instead, and set it to either first or last name accordingly, then sort on that normalized key. To sort conditionally with WP_Query— that is, to ignore my advise above and try to make WP_Query/SQL do this– … Read more

Foreach displays from last to first

Why wouldn’t you just pull them in the order you need instead of reversing the array? $args = array( ‘post_type’ => ‘attachment’, ‘posts_per_page’ => -1, // correct key is posts_per_page, not numberposts ‘orderby’ => ‘date’ ‘order’ => ‘ASC’, ‘post_parent’ => $post->ID ); $attachments = get_posts( $args );

What file have I to create in my custom WordPress theme to show all the post belonging to a specific category?

A custom template for one specific category should be named category-slug.php where “slug” is the WP slug for the category. WordPress will automatically use this file to display the archive for that one category. You could also name it using the ID of the category. If your category is “legacy-posts”, then your template file should … Read more

Why isn’t my multiple loops working?

This seems to work: while( have_posts() ): the_post(); the_content(); endwhile; $my_query = new WP_Query(‘page_id=40’); while ($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate = $post->ID;?> <div class=”entry”> <?php the_content(‘read more &raquo;’); ?> </div> <?php endwhile;

Escape current post from loop

The title of question is inaccurate. I think you don’t want to escape first post, instead you want to exclude current post. You can use the post__not_in argument in WP_Query. $args = array( ‘s’ => ‘searchTerm’ ); //Check if we are in a post of any type if( is_singular() ) { $post = get_queried_object(); $args[‘post__not_in’] … Read more

How to overwrite orderby with add_query_var

Use pre_get_posts instead.. function my_archive_query_override( $query ) { if ( $query->is_archive() && $query->is_main_query() ) { $query->set( ‘orderby’, ‘rand’ ); } } add_action( ‘pre_get_posts’, ‘my_archive_query_override’ ); For that user selectable part, you can use AJAX to accomplish that.

Custom taxonomy archive page problem

There are many choices here, and it is all up to you what you want to achieve and how you want to achieve it. My idea would be to use the templates available to avoid a lot of if else conditional statement. The Template Hiearchy makes provision for templates for terms, taxonomy-{$taxonomy}-{$term}.php, so you could … Read more