Add custom field (value) to search result (without plugin)

WordPress search in posts table. You must change the search query by adding a postmeta table (with LEFT JOIN) and then add your own conditions to WHERE clause. <?php /** * Extend WordPress search to include custom fields * * https://adambalee.com */ /** * Join posts and postmeta tables * * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_join */ function cf_search_join( … Read more

Using is_main_query to select custom post type on certain page

The is_home() conditional returns true when the currently displayed page is the blog posts index. If you want to target the site front page specifically instead, you need to use is_front_page(): function wpse83754_filter_pre_get_posts( $query ) { if ( $query->is_main_query() && is_front_page() ) { $query->set( ‘post_type’, array( ‘home_portfolio’ ) ); } } add_action( ‘pre_get_posts’, ‘wpse83754_filter_pre_get_posts’ ); … Read more

pre_get_posts on a page

Explaining the object. You don’t have to manually force data into those class properties. The whole $wp_query object has a bunch of internal methods that you should use to set values. Here’s an example: public function query( $query ) { if ( ‘SOME_POST_TYPE’ !== $query->get( ‘post_type’ ) OR ! $query->is_main_query() OR ! $query->is_archive() OR ! … Read more

RSS feed with specific keyword

You can modify the list of posts displayed in feed using pre_get_posts action. And to target only feeds, you can use is_feed conditional tag. So such code can look like this: add_action( ‘pre_get_posts’, function ( $query ) { if ( ! is_admin() && is_feed() && $query->is_main_query() ) { if ( isset($_GET[‘q’]) && trim($_GET[‘q’]) ) { … Read more

“pre_get_posts” firing on every query

Basically what you are looking for is the global $wp_the_query variable which is set to the value of the main query. It may not be a perfect fit for 100% of cases but will probably work fine in 99% of cases: add_action( ‘pre_get_posts’, ‘custom_post_count’ ); function custom_post_count( $query ){ global $wp_the_query; if ( $wp_the_query === … Read more

Slow SQL_CALC_FOUND_ROWS Query

The use of SQL_CALC_FOUND_ROWS is not really a problem, although it incurs an overhead. What happens is, WordPress uses SQL_CALC_FOUND_ROWS in order to determine the total posts that would have been returned, if no LIMIT clause was provided. This is necessary in order to calculate and provide you with correct pagination links. Disabling it unconditionally … Read more

Sort archive view with pre_get_posts hook

I solved this with a different approach. I used posts_join and posts_orderby filters. Do not forget to cast meta value as unsiged to avoid string ordering instead of int. //edit query join function hsa_story_join($join){ global $wpdb; $option = get_option(‘hsa_story_cats’); if(!is_admin() && is_category($option)) { $current_category = get_queried_object(); $join .= ” LEFT JOIN (SELECT post_id, meta_key, meta_value … Read more

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