How to query ‘posts_per_page’ to display a different blog posts index template?

You can control what template loads for any type of query via the Template Filters. Here’s an example using home_template that checks if posts_per_page is equal to 1, and loads single.php in that case. function wpd_home_template( $home_template=”” ){ if( get_option( ‘posts_per_page’ ) == 1 ){ $home_template = locate_template( ‘single.php’, false ); } return $home_template; } … Read more

using wp_query to return posts w/ comment count > 0

While the WP_Query class has the native possibility to orderby the comment_count, it doesn’t have the same to query based on those. But when we look at the posts-table, we can see that there isn’t much we need to alter ID | post_author | post_date | post_date_gmt | post_content | post_title | post_excerpt | post_status … Read more

Injecting content with $wp_query->current_post restarts from zero on paged pages. How to inject content after X posts, regardless of pagination?

For the record, I just found a new/different way to accomplish this goal. It’s not quite the same, but it does allow me to insert a new post type after every X posts in my original loop. This is the source: https://www.gowp.com/blog/inserting-custom-posts-loop/ The method was done via the code below, which essentially injects a new … Read more

Get posts from the Main site to sub site – WordPress MultiSites Network

I use this codes to loop over the posts in the Main site: <?php $sites = wp_get_sites(); foreach($sites as $site) : // Only subsites // Connect to new multisite switch_to_blog($site[‘blog_id’]); $case_args = array( ‘post_type’ => ‘downloads’, ‘posts_per_page’ => 1, ‘orderby’ => ‘date’, ‘order’ => ‘DESC’, ); $latest_cases = new WP_Query($case_args); if ( $latest_cases->have_posts() ) : … Read more

Can not switch the queried post in pre_get_posts hook

Huh. I double-checked. On my local server, this bit of code successfully changes the post: function test_redirect() { global $wp_query; $wp_query->set(‘page_id’, 5); } add_action(‘pre_get_posts’, ‘test_redirect’); You’re going to have to do a bit of debugging. On a dev server, var_dump the $wp_query variable a few key places to check that it has what you expect … Read more

cloning a WP_Query

You shouldn’t have to “get your query back”. Your queries are all stored in different objects– $team, $reports_played, the original $wp_query, etc. Just access the particular object that you need to use. This is untested but to get functions like the_title working, run $query_object_name->the_post() at the top of each Loop iteration and $post = $parent_object_name->post … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)