number of posts per page + random + exclude categories

pre_get_posts in your child themes functions file is the best in my opinion. add_action( ‘pre_get_posts’, ‘wpsites_custom_get_posts’ ); function wpsites_custom_get_posts( $query ) { if( $query->is_main_query() && $query->is_home() ) { $query->set( ‘cat’, ‘-182,-184’ ); $query->set( ‘posts_per_page’, ’12’ ); $query->set( ‘orderby’, ‘RAND’ ); } } This method is more flexible in my opinion.

Another query in pre_get_post cause memory issue

Remove the action before you run the internal query, and always verify main query with is_main_query: function custom_loops($query) { if ( $query->is_tax( ‘service_photo_location’ ) && $query->is_main_query() ){ remove_action( ‘pre_get_posts’, ‘custom_loops’ ); $ids = get_nearby_partners_by_area(50000, ‘service_photo’); // … } } add_action(‘pre_get_posts’, ‘custom_loops’);

pre_get_posts variables

The logic inside the is_home function relies on the main query, but by definition that query hasn’t happened yet as you’re still in the pre_get_posts filter. Instead, consider $q->is_home(). The same goes for is_feed() $q->is_feed()

Hide elements outside loop based on query

You can use the function wp_list_pluck to get the ID of the posts retrieved from the query. Just move the query before the h2 element and get the posts ID’s: // Set a bool to know if at least one post has post thumbnail. $has_thumb = false; $first_query = new WP_Query(‘cat=1&author=” . $post->post_author . “&order=DESC&tag=Tattoo&posts_per_page=1000’); … Read more

Automatically applying a pre_get_posts filter for child categories only

Try this: EDIT: added short circuit return statements to avoid running unnecessary code on admin pages or other queries (e.g. nav menu items) // Custom query for research sub-categories function custom_query_for_research_subcategories($query) { if (is_admin() || !$query->is_main_query()) { return $query; } $qobj = get_queried_object(); if (isset($qobj->taxonomy) && ‘category’ == $qobj->taxonomy ) { $term = get_term($qobj->term_id); if … Read more

How to change the amount of posts previewed on a page?

You’re missing an s. $query->set(‘posts_per_page’, ‘1’); However – you probably don’t want to do this for every query, since that affects everything – widgets, back end, everything. You should make it conditional. For example: <?php function university_adjust_queries($query) { // If this is the main query, not in wp-admin, and this is the homepage if($query->is_main_query() && … Read more

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