Custom WP Query from meta_value stored as serialised array

If you’re inside the while loop, use get_the_ID(). // returns an array as this is a multicheck field in Magic Fields 2 if(get_post_meta(get_the_ID(), ‘services’, true)){ $services = get_post_meta(get_the_ID(), ‘services’, true); $services = maybe_unserialize($services); // foreach item in the array foreach ($services as $service) { if ( sanitize_title($service_title) == sanitize_title($service) ) { echo ‘<p>’ . esc_attr($service) … Read more

wp_query custom post type by taxonomy & standard post by taxonomy

here is the answer: $args = array( ‘post_type’ => array( ‘artists’, ‘post’ ), ‘post_status’ => ‘publish’, ‘tax_query’ => array( ‘relation’ => ‘OR’, array( ‘taxonomy’ => ‘tag_artists’, ‘field’ => ‘slug’, ‘terms’ => array( ‘home’ ) ), array( ‘taxonomy’ => ‘post_tag’, ‘field’ => ‘slug’, ‘terms’ => array( ‘home’ ) ) ) ); $query = new WP_Query( $args … Read more

Custom Post Order for a Custom Post Type

As it’s not clearly stated, how (in code) the “related” CPTs are interconnected, I want to post a mostly overseen possibility for CPTs. When you register_post_type(), you have the argument of hierarchical that should be set to true to allow for ‘supports’ => array( ‘page-attributes’ ). This then allows you to define an order manually.

How to use Meta Value Compare in WP_Query For Max and Min numbers

This looks right, I have very similar code that works as expected: function price_query_test() { update_post_meta( 7, ‘_price’, 10 ); update_post_meta( 12, ‘_price’, 20 ); update_post_meta( 18, ‘_price’, 30 ); update_post_meta( 72, ‘_price’, 40 ); $test = new WP_Query( array( ‘post_type’ => ‘page’, ‘meta_query’ => array( array( ‘key’ => ‘_price’, ‘value’ => array( ’10’, ’25’ … Read more

wp_query() get_col error

UPDATE, Correct Answer: The core WordPress functions are inaccessible at time when using AJAX calls. The proper way to handle this is covered in “AJAX in Plugins” on the Codex. Original answer that didn’t solve the issue but might be interesting for others some day. Here’s an alternate way that avoids having to directly query … Read more

Query by custom dates in UNIX Time

PHP has a built-in date() function that formats Date-objects. In your case, you would use it as follows: echo date(‘Y’, 1322697600); Since you’re using these query arguments to build up an array of posts, and you want to filter specifically on that, I’d recommend building a function that triggers the loop based on your desired … Read more

Pagination stops at page 6

Trying to use pagination from a different query is always fraught with potential disaster. next_posts_link accepts a second argument, $max_pages, which may solve your issue if you pass it properly: next_posts_link( __( ‘Next’), $idxloop->max_num_pages ); However, the real answer to this question is to adjust your query before the template. Adjusting the default query via … Read more