WP_Query get posts from custom posts by category

You’re using category query for your custom taxonomy. So it won’t work, because there are no categories with such IDs. You should use tax_query instead. // post types query while ( have_posts() ) { the_post(); // getting the categories $categoryIDs = array(); // it’s much nicer than concatenated string $getcategory = get_the_terms($post->ID, ‘custompostnamehere-categories’); foreach($getcategory as … Read more

Related posts in all categories of current post (custom post types)

This is the code that was needed in order to make it show only the posts (that were of a specific custom post type) that matched the same child category (therefore matching all the other categories): <!– other posts –> <?php //this gets the lowest hierarchical child term for current post $categories = get_the_category($post->ID); foreach($categories … Read more

Storing query variable and then using wp_reset_query

wp_reset_query(); resets the query to the original main query of the page. Therefore, storing the query in another variable is redundant and unnecessary. Updated code: $args = array( ‘cat’ => 15, ‘paged’ => $paged ); $custom_query = new WP_Query( $args ); while ($custom_query -> have_posts()) : $custom_query -> the_post(); endwhile; my_pagination(); //call function wp_reset_query(); //#3. … Read more

Get random posts between specific dates / of specific age

Two notes beforehand: There is no (unless you created a CPT) post type random_posts. Don’t use query_posts. That being said, the following will do what you want: Just the randomness $args = array( ‘posts_per_page’ => ’10’, ‘orderby’ => ‘rand’ ); $random_posts = get_posts( $args ); foreach( $random_posts as $random_post ) { // do something echo … Read more

How to query for posts without a post_format

You have to query post that has no post format attached: $args = array( ‘post_type’ => ‘post’, ‘tax_query’ => array( array( ‘taxonomy’ => ‘post_format’, ‘field’ => ‘slug’, ‘operator’ => ‘NOT IN’, ‘terms’ => get_terms(‘post_format’) ) ) ); To improve performance you can hardcoding the post format instead of using get_terms to retrieve them: $args = … Read more

How to reduce the number of queries?

Try this: global $wpdb; $num_cat = 6; $query = “SELECT tt.term_taxonomy_id, t.name, COUNT( t.name ), GROUP_CONCAT( p.ID ) , GROUP_CONCAT(m.meta_value) FROM {$wpdb->prefix}term_taxonomy tt NATURAL JOIN {$wpdb->prefix}terms t NATURAL JOIN {$wpdb->prefix}term_relationships tr JOIN {$wpdb->prefix}posts p ON ( tr.object_id = ID ) JOIN {$wpdb->prefix}postmeta m ON ( post_id = ID ) WHERE taxonomy = ‘category’ AND post_type=”post” … Read more

WPDB query – decrypting DB data

It’s just serialized, in WordPress, you can run maybe_unserialize and get back the variable/array. $crosssells = $wpdb->get_results( “SELECT * FROM $wpdb->postmeta WHERE _crosssell_ids <> ” ” ); $array = maybe_unserialize($crosssells); However, there are built in functions to retrieve posts based on meta information. You can use get_posts to retrieve the posts and get_post_meta to retrieve … Read more