WP_Post_List_Table::get_views – Have post counts account for filters?

You might want to remove those counts and replace them with your own. function insert_post_counts($views){ //Run your query to count posts //use wp_cache_set and wp_cache_get to optimize performance $edit_url = admin_url( ‘edit.php’ ); $views[‘all’] = ‘All <a href=”‘.$edit_url.'”>(‘.$all_count.’)</a>’; $views[‘publish’] = ‘Published <a href=”‘.$edit_url.’?post_status=publish”>(‘.$publish_count.’)</a>’; $views[‘draft’] = ‘Draft <a href=”‘.$edit_url.’?post_status=draft”>(‘.$draft_count.’)</a>’; $views[‘trash’] = ‘Trash <a href=”‘.$edit_url.’?post_status=trash”>(‘.$draft_count.’)</a>’; return $views; … Read more

How-to exclude terms from the main query the most performant way?

You can set the taxonomy query for the main query using pre_get_posts: add_action( ‘pre_get_posts’, ‘my_exclude_terms_from_query’ ); function my_exclude_terms_from_query( $query ) { if ( $query->is_main_query() /* && whatever else */ ) { $tax_query = array ( array( ‘taxonomy’ => ‘category’, ‘terms’ => array( ‘cat-slug’ ), ‘field’ => ‘slug’, ‘operator’ => ‘NOT IN’, ) ); $query->set( ‘tax_query’, … Read more

pre_get_posts with get_posts

Firstly, you are invoking an infinite loop, which causes the memory exhaustion. To avoid it, put the following at the beginning of your function: // avoid infinite loop remove_action( ‘pre_get_posts’, __FUNCTION__ ); It makes sure the you are not hooking it into pre_get_posts over and over again, re-initiating your get_posts() call over and over again. … Read more

Archive Listings Filtered by Date Values in a Custom Field/Post Meta?

As with many things in WordPress there are several ways to do what you want. I’m going to explain one of them. Remove the ‘year’, ‘monthnum’ and ‘day’ Query Variables You can modify the parameters to the query WordPress uses on the archive URLs inside the ‘pre_get_posts’ hook. Those parameters are captured as an associative … Read more

Using pre_get_posts to set posts per page, how do I?

You’re almost there mate. Try this though. <?php add_action(‘pre_get_posts’, ‘filter_press_tax’); function filter_press_tax( $query ){ if( $query->is_tax(‘press’) && $query->has_term(‘press’)): $query->set(‘posts_per_page’, 5); return; endif; } ?> You can use any conditional tag or any argument that can be passed to WP_Query to test your condition or set a new value via pre_get_posts. Also try $query->get(‘taxonomy’) / $query->get(‘term’). … Read more

complex restriction of items in media library

Good code. I think you could simplify by making direct SQL queries instead: — Featured Images SELECT `meta_value` FROM `wp_postmeta` WHERE `meta_key` = ‘_thumbnail_id’; –> $thumbnail_ids — Header Images SELECT `post_id` FROM `wp_postmeta` WHERE `meta_key` = ‘_wp_attachment_context’ and `meta_value` = ‘custom-header’; –> $header_ids Also, with your method, I think that if you call WP_Query with … Read more

Order Search Results Page by meta_value If no Value Return Remaining Results

You can accomplish this with multiple meta queries. $query->set( ‘meta_query’, [ ‘relation’ => ‘OR’, ‘wpcf-start-date’ => [ ‘key’ => ‘wpcf-start-date’, ‘compare’ => ‘EXISTS’, ], ‘no-start-date’ => [ ‘key’ => ‘wpcf-start-date’, ‘compare’ => ‘NOT EXISTS’ ], ] ); $query->set( ‘orderby’, ‘wpcf-start-date’ ); $query->set( ‘order’, ‘ASC’ ); This will tell WP to create a query that will … Read more

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