List all custom post type posts from a given category?

What about doing a tax_query? $args = array( ‘post_type’ => ‘myposttype’, ‘tax_query’=> array(         ‘taxonomy’ => ‘myposttype_categories’,         ‘terms’ => array(‘foo’),         ‘field’ => ‘slug’,     ) ); $loop = new WP_Query( $args ); var_dump($loop); Facepalm question, you are sure that these taxonomies/post types exist and that there are posts filed under them? Update The query seems to work … Read more

Count posts returned by get_posts in external PHP script

The WordPress function get_posts() is making it’s own instance of WP_Query that is not globally accessible: function get_posts($args = null) { // … cut … $get_posts = new WP_Query; return $get_posts->query($r); } so you could instead try $results = get_posts($args); echo count($results); to give you the array count of post objects returned by get_posts(). WP_Query() … Read more

exclude custom post type by meta key in wp_query

If you don’t use any other meta data, just add the following to your $args: ‘meta_key’ => ‘sold’, ‘meta_value’ => true, // or whatever it is you’re using here ‘meta_compare’ => ‘!=’, Otherwise, use a WP_Meta_Query: ‘meta_query’ => array( array( ‘key’ => ‘sold’, ‘value’ => true, // or whatever it is you’re using here ‘compare’ … Read more

Sort by meta key on archive page

Use pre_get_posts to alter the main query before it is run. The action runs on every query, use the Conditional Tags to target specific queries. function wpd_sort_by_meta( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( ‘meta_key’, ‘your_meta_key’ ); $query->set( ‘orderby’, ‘meta_value_num’ ); } } add_action( ‘pre_get_posts’, ‘wpd_sort_by_meta’ );

Trim first 2 words of the exceprt

A more reliable way would be to filter the excerpt and to explode the string into an array, remove the first two key/value pairs from the array and then return your string add_filter( ‘wp_trim_excerpt’, function ( $text ) { // Make sure we have a text if ( !$text ) return $text; $text = ltrim( … Read more

Unable to display multiple post types in same query (WPML WP_Query)

I had this problem with querying on two custom post types, using WP_Query. I had no problem querying for one type or the other in the array, but not both at the same time. Did not work: $args = array( ‘post_type’ => array(‘custom_type_1′,’custom_type_2’), ‘posts_per_page’ => 4 ); Did work: $args = array( ‘post_type’ => array(‘custom_type_1’), … Read more