How to change main query based on post meta

Finally got it. This code in functions.php: function set_order_by_last_date($query){ $query->set( ‘orderby’, ‘eventstart’); $query->set( ‘order’, ‘DESC’ ); $query->get_posts(); if ($query->have_posts()){ if ( eo_get_the_end(‘Ymd’,$query->posts[0]->ID,$query->posts[0]->occurrence_id) > date(‘Ymd’) ){ $query->set( ‘event_end_after’, ‘today’ ); $query->set( ‘orderby’, ‘eventstart’); $query->set( ‘order’, ‘ASC’ ); } else { $query->set( ‘event_end_before’, ‘today’ ); $query->set( ‘orderby’, ‘eventstart’); $query->set( ‘order’, ‘DESC’ ); } } return $query->query_vars; } … Read more

Store sticky post’s ids in a transient

You can try: add_action(‘update_option_sticky_posts’, function( $old_value, $value ) { $featured_args = array( ‘post__in’ => $value, ‘post_status’ => ‘publish’, ‘no_found_rows’ => true ); // The Featured Posts query. $featured = new WP_Query( $featured_args ); // Proceed only if published posts with thumbnails exist if ( $featured->have_posts() ) { while ( $featured->have_posts() ) { $featured->the_post(); if ( … Read more

Show all posts even if URL points to a single one

I ended up using the request filter, just like in the documentation: function filterRequest( $request ) { global $single_post_slug; $dummy_query = new WP_Query(); // the query isn’t run if we don’t pass any query vars $dummy_query->parse_query( $request ); if( $dummy_query->is_single() && !$dummy_query->is_admin() ) { $single_post_slug = $request[‘name’]; $request[‘name’] = “”; $request[‘category_name’] = “”; } return … Read more

Pre get posts where template is not equal to one specified?

You should use meta_query to pass meta query arguments. add_action(‘parse_query’, ‘se334731_filter_admin_post_list’); function se334731_filter_admin_post_list( $query ) { $screen = get_current_screen(); if ( is_admin() && $screen->post_type == ‘page’ && $screen->base == ‘edit’ ) { $query->query_vars[‘meta_query’][] = [ ‘relation’ => ‘OR’, [ ‘key’ => ‘_wp_page_template’, ‘value’ => ‘page-some-template.php’, ‘compare’ => ‘!=’, //’value’ => array( ‘page-some-template.php’ ), //’compare’ => … Read more

Change archive page template using pre_get_post

You corrected the filter hook name from archive-videos to template_include, but in the first function return $query; statement comes before add_filter(), so the filter is not added. add_action( ‘pre_get_posts’ ,’post_type_videos’ ); function post_type_videos( $query ) { if ( ! is_admin() && $query->is_post_type_archive( ‘videos_cpt’ ) && $query->is_main_query() ) { $query->set( ‘post_type’, ‘videos’ ); //set query arg … Read more

How to do multiple searches (with logical OR) in WP_Query in hook pre_get_posts?

You could do something like this add_action(‘pre_get_posts’, ‘bt_search_posts’); function bt_search_posts ($query) { // the posts ids that contain foo bar OR lorem ipsum dolor OR ping pong $posts_ids = []; // get the current query vars $query_vars = $query->query_vars; // loop each term we want to search and if found, add the post id into … Read more

Distinction on meta value on pre_get_posts

DISCLAIMER : Not a WordPress Expert, Just an old MySQL DBA Here is your original generated SQL SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) WHERE 1=1 AND wp_posts.post_type=”post” AND (wp_posts.post_status=”publish” OR wp_posts.post_status=”private”) AND ( ( wp_postmeta.meta_key = ‘_position’ AND CAST(wp_postmeta.meta_value AS CHAR) > ‘0’ ) ) GROUP BY wp_posts.ID, wp_postmeta.meta_value … Read more

Force is_search to always TRUE

You can force WordPress to load search.php template when you need it by using the following code: add_filter( ‘template_include’, ‘get_search_template’ ); instead of $q->set( ‘is_search’, true );

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