Show recent products first but “sold out last” in query

If we only have two stock statuses, namely outofstock and instock, we can very easily achieve sorting with pre_get_posts add_action( ‘pre_get_posts’, function ( $q ) { if ( !is_admin() // Target only front end && $q->is_main_query() // Only target the main query && $q->is_post_type_archive() // Change to suite your needs ) { $q->set( ‘meta_key’, ‘_stock_status’ … Read more

wp remove query

I’m assuming you’re talking about this piece of maintenance code right here: http://core.trac.wordpress.org/browser/tags/3.3.1/wp-admin/includes/post.php#L419 Indeed, the code calls on lower level functionality of $wpdb, which doesn’t contain too many hooks, only one in fact, query. http://core.trac.wordpress.org/browser/tags/3.3.1/wp-includes/wp-db.php#L1061 So, in order to return an empty result set you’ll have to come up with a no-operation type of query, … Read more

Insert html after certain amount of posts?

You could use the following and it should do exactly what you want by checking the value of $loop->current_post. <?php $loop = new WP_Query( array( ‘post_type’ => ‘work’,’posts_per_page’ => ‘-1’ ) ); ?> <ul id=”carousel”> <li> <ul class=”inner-items”> <?php while ( $loop->have_posts() ) : $loop->the_post(); ?> <?php if( $loop->current_post && !($loop->current_post % 6) ) : … Read more

How to get category link without a database query

It’s down to how you’re using get_term_link() – since you’re passing a slug, WordPress can’t locate the term in it’s internal cache (terms are indexed by ID), so it grabs it directly from the db. To use the cache, pass the ID. Better yet, pass just the object: get_term_link( $category ); // No need for … Read more

Different Main Navigation per category

You could set this in your theme using conditionals. Create a separate menu for each category using the WordPress menu feature (Appearances > Menus). Then in your theme, where you want the menu to display, determine which menu to display using the is_category(‘category-slug’) and/or in_category(‘category-slug’) conditionals and the wp_nav_menu function (http://codex.wordpress.org/Function_Reference/wp_nav_menu). For instance: <?php if(is_category(‘first-category’) … Read more

When add_query_arg() is necessary?

The difference is, in “plugin_part1-fixed.php” you wouldn’t lose existing args. Consider the url: https://www.example.com/page/?foo=bar Your code in “plugin_part1.php” would output <a href=”https://www.example.com/page/?custom_var=column”>text</a> Note, the existing arg “foo” has been lost, the code in “plugin_part1-fixed.php” would output: <a href=”https://www.example.com/page/?foo=bar&custom_var=column”>text</a> Note, the existing arg “foo” is still present and your arg has been appended. Since you don’t … Read more

How to extract all ID variables from a query string?

$string will contain a comma separated list of IDs: $ids = array(); foreach( $array as $post ) { if( !in_array( $post->ID, $ids ) ) { $ids[] = $post->ID; } } $string = implode( ‘, ‘, $ids ); Note: This uses basically no wordpress, other than the post object…which is really just an object.

is_archive() doesn’t work on public query var archive pages?

Try this: add_action( ‘template_redirect’, ‘my_test_if_archive’ ); function my_test_if_archive() { global $wp_query; $qv = array_keys( $wp_query->query ); $archives = array(‘year’, ‘monthnum’, ‘day’, ‘w’, ‘m’, ‘author’, ‘post_type’); $is_archive = ! empty( array_intersect( $qv, $archives ) ); $is_tax = ! empty( $wp_query->tax_query->queries ); if ( $is_archive || $is_tax ) { // this is an archive } }

$query->query_var[‘post_type’] not set for pages

To clear up some confusion in previous answers, pre_get_posts isn’t a filter so you don’t need to return anything. Your only problem that I see is the if: if ( !is_search() && !in_array( get_post_type(), $post_types ) ) return; Basically get_post_type() is going to return false during pre_get_posts because the global $post hasn’t been set yet … Read more