Serialized array, grab specific posts with meta_key/meta_value[0]->is_featured

It is possible if you shape your meta query to look for the serialized string like this, assuming your array value is always an integer: $params = array( ‘meta_query’ => array( array( ‘key’ => ‘mediSHOP_product_extras’, ‘value’ => ‘s:11:”is_featured”;i:1;’, ‘compare’ => ‘LIKE’ ) ) ); $query = new \WP_Query( $params ); If the array value is … Read more

What’s the difference between “get_posts” and “wp_get_recent_posts” when used with “setup_postdata”?

If you look at the source of setup_postdata() you’ll find that it requires an object ($post), to be passed, not an array. wp_get_recent_posts() (source), by default (for pre 3.1 backwards compatibilty) returns each post as an array. The second, optional argument, that can be passed to wp_get_recent_posts() can prevent this: $posts = wp_get_recent_posts( $args, OBJECT_K … Read more

SELECT * FROM $wpdb->posts WHERE ID > 160

SELECT * FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id) LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id) LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id) WHERE $wpdb->posts.post_type=”post” AND $wpdb->term_taxonomy.taxonomy = ‘post_tag’ AND $wpdb->posts.ID > 160 ORDER BY $wpdb->posts.ID ASC LIMIT 5 $pageposts = $wpdb->get_results($querystr); if ($pageposts): ?> <?php global $post; ?> <?php … Read more

get_posts empty when called via Ajax

To get data from AJAX call you have to echo or print it. In WordPress you should use wp_send_json() function to send data as response to call. function mh_get_addons( $args = array() ) { $defaults = array( ‘posts_per_page’ => -1, ‘orderby’ => ‘post_title’, ‘order’ => ‘DESC’, ‘post_type’ => ‘mh-addon’, ‘post_status’ => ‘publish’ ); $args = … Read more

get_posts from post x(offset=>x) to end

It’s strange, you’d have thought if you disable paging and grab all posts, that you could naturally set an offset to, eg. array( ‘nopaging’ => true, ‘offset’ => 10 ) or array( ‘posts_per_page’ => -1, ‘offset’ => 10 ) or array( ‘numberposts’ => -1, ‘offset’ => 10 ) This unfortunately doesn’t appear to work(bug / … Read more

meta_query date and time comparisons

You are mixing up an older meta syntax with the newer one. I’d suggest you clean that up for readability and for potential compatibility in the future. (WordPress does seem to parse the mixed query correctly though) $args = array( ‘posts_per_page’ => -1, ‘post_type’ => MDJM_EVENT_POSTS, ‘post_status’ => ‘mdjm-approved’, ‘meta_query’ => array( ‘relation’ => ‘AND’, … Read more

Get random custom posts from a custom post type

You need to include setup_postdata($post); in your foreach line. Here’s some great demo code from the codex, adopted to fit your query: <ul> <?php global $post; $tmp_post = $post; $myposts = get_posts( ‘post_type=predic&numberposts=4&orderby=rand’ ); foreach( $myposts as $post ) : setup_postdata($post); ?> <li><a href=”https://wordpress.stackexchange.com/questions/74243/<?php the_permalink(); ?>”><?php the_title(); ?></a></li> <?php endforeach; ?> <?php $post = $tmp_post; … Read more