querying with custom meta field with meta_query

There’s actually a better solution that will (hopefully) be rolling out in WordPress 3.4 — you can run the patch as a hotfix now if you’d like, but here’s the TRAC link for the patch: http://core.trac.wordpress.org/ticket/18158 With this, you can do … $my_query = new WP_Query( array( ‘meta_query’ => array( array( ‘key’ => ‘foo’, ‘compare’ … Read more

Grab 5 latest posts from custom post type ‘announcements’

The following should work, but isn’t tested: <div id=”header-announcements”> <h3>Announcements</h3> <?php $queryObject = new WP_Query( ‘post_type=announcements&posts_per_page=5’ ); // The Loop! if ($queryObject->have_posts()) { ?> <ul> <?php while ($queryObject->have_posts()) { $queryObject->the_post(); ?> <li><a href=”https://wordpress.stackexchange.com/questions/26482/<?php the_permalink(); ?>”><?php the_title(); ?></a></li> <?php } ?> </ul> <div><a href=”#”>View More</a></div> <?php } ?> </div>

wp_query order by title

If you’re trying to get them to order by ascending price, the problem is that ordering by title is a string comparison, not numeric. A solution would be to put the price in a custom field and orderby your custom field rather than title. You would add a meta_key param with the name of your … Read more

get_posts not finding argument: post_name

Aha, despite there being a postmeta field of post_name->’slug’, the correct syntax (which mirrors that of WP_Query, is $args = array( ‘name’ => $postSlug, ‘post_type’ => ‘post’, ‘post_status’ => ‘publish’ ); $slugPosts = get_posts($args); Where ‘name’ is the key in the query (for posts, ‘postname’ for page). Doesn’t make much sense but might as well … Read more

How to Access Global $multipage or Global $numpages outside the loop?

Just inspect the current post content for ‘<!–nextpage–>’: function wpse_check_multi_page() { $num_pages = substr_count( $GLOBALS[‘post’]->post_content, ‘<!–nextpage–>’ ) + 1; $current_page = get_query_var( ‘page’ ); return array ( $num_pages, $current_page ); } On page 2 of 3 that returns: Array ( [0] => 3 [1] => 2 ) On an unpaged post it returns: Array ( … Read more