2 wordpress loops showing 1 post from same post type – how to avoid showing the same post?

Your two queries are identical, meaning you only need one of them. The basic solution is: global $testimonials; $testimonials = new WP_Query(array( ‘posts_per_page’ => 2, /* <– get both */ ‘post_type’ => ‘testimonial’, ‘post_status’ => ‘published’, ‘orderby’ => ‘rand’, ‘ignore_sticky_posts’ => true /* you need this to control post count accurately */ )); // header … Read more

Query multiple tables at once?

SELECT * FROM wp_posts, wp_2_posts, wp_3_posts WHERE wp_posts.post_content LIKE ‘%foo’ OR wp_2_posts.post_content LIKE ‘%foo’ OR wp_3_posts.post_content LIKE ‘%foo’; should do the trick. (Note that in Multisite, there shouldn’t be a wp_1_posts — your root site uses wp_posts etc.)

How to get posts and comments amount per hour, per year and per month?

For comments: function count_comments_in_period( $date_from, $date_to ){ global $wpdb; $count = $wpdb->get_var( $wpdb->prepare(“SELECT COUNT(*) FROM {$wpdb->comments} WHERE comment_date >= %s AND comment_date < %s “, $date_from, $date_to)); return $count; } Just use dates in yyyy-mm-dd format. For posts: function count_posts_in_period( $date_from, $date_to ){ global $wpdb; $count = $wpdb->get_var( $wpdb->prepare(“SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_date >= … Read more

Passing parameters to a static front page

front-page.php is a static front page (if you rename your current template front-page.php, and don’t select nothing in Settings->Reading it works exactly like now. However, if you want to use the page approach and you don’t use mcdata for query, remove the filter you posted, and simply retrieve the mcdata value in your template using … Read more

Dynamically switch template on click

My answer is something like an application of one of the @allenericr answer points + some cache. I suggest to implement this only if the 3 templates are different in the content and not only in the aspect, otherwise a CSS + JS solution should be the best choice. Create a main page template ‘multi-template-page.php’ … Read more

Modify Search Query if original Query gave no results

You should create a custom query inside if. if ( $total_results == 0 ) { // Create a new query with the terms ‘Find Us’ $query = new Wp_query(‘s=Find Us’); if ($query->have_posts()): while ($query->have_posts()):$query->the_post(); //whatever you want to do with the result endwhile; endif; wp_reset_postdata(); } You can refer Creating a search page and Wp_query … Read more

Query only the posts with a post format of “audio”

Before we continue, I must point out this: $i=1; $temp = $wp_query; $wp_query = null; $wp_query = new WP_Query(); $wp_query -> query(‘post-format-audio’.’&paged=’.$paged) This is exactly the same as: query_posts( ‘post-format-audio’.’&paged=’.$paged ); Which is terrible. When people advise against using query_posts, it’s not just the function that’s bad, it’s what it does. Thankfully someone told you … Read more