Loop for custom post types filtered by a taxonomy

I’ve figured out the answer using the WordPress forum. Solution below: <?php query_posts( array( ‘country’ => ‘event-england’ ) ); ?> <?php if( is_tax() ) { global $wp_query; $term = $wp_query->get_queried_object(); $title = $term->name; } ?> <ul> <span class=”tax-title”><?php echo($title); ?></span> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <li><a href=”https://wordpress.stackexchange.com/questions/17046/<?php … Read more

Exclude a category from a query that includes its parent category

First you should move wp_reset_query(); outside of the loop instead of resetting the query every post of the loop. And as for your query you can use category__not_in $args = array( ‘cat’ => 14, ‘category__not_in’ => array(’45’), ‘posts_per_page’ => 20 ); $the_query = new WP_Query($args); while ($the_query->have_posts()) : $the_query->the_post();?> <li> <a href=”https://wordpress.stackexchange.com/questions/17387/<?php the_permalink() ?>” rel=”bookmark” … Read more

Query posts to get all but the most recent post

I had to use the WP_Query class: $the_query = new WP_Query(array( ‘category_name’ => ‘Recipe of the Week’, ‘offset’ => 1 ) And then reference my new query object in the template like this: <?php while ( $the_query-> have_posts() ) : $the_query->the_post(); ?>

query_post order desc

You need to use this to parameters orderby and order <?php query_posts( array( ‘category_name’ => ‘my_category_slug’, ‘orderby’ => ‘ID’, ‘order’ => ‘DESC’, ‘posts_per_page’ => -1 ) ); ?>

Filter Loop by Custom Field Value

How about this: $my_query = new WP_Query(array( ‘post_type’=> ‘custom_type’, ‘post_status’ => ‘publish’, ‘meta_key’ => ‘my_custom_field’, ‘meta_value’ => ‘custom field value’ )); if($my_query->have_posts()): while($my_query->have_posts()):$my_query->the_post(); //All the post stuff here. endwhile; endif; wp_reset_postdata(); Check this out.

Query_posts with custom field meta value

The best solution i found for querying posts where the meta_key IS NOT or doesn’t exists is to use the posts_where filter with a small sub-query, try this: //paste this function in your theme’s functions.php function metakey_no_featured( $where ) { global $wp_query; global $wpdb; $where .= $wpdb->prepare(” AND $wpdb->posts.ID NOT IN ( SELECT post_id FROM … Read more

why to use query_posts() in custom page?

See When should you use WP_Query vs query_posts() vs get_posts()? For a custom post type or a simple page you don’t need query_posts(). Usually, you almost never need it, because it will replace the default query which leads to some counter-intuitive side effects. When in doubt use get_posts().