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

Perform query with meta_value date

I did something similar, the technique you need to use is called a meta query. Here is the query I wrote to get posts based on a date value stored as a custom field meta value. query_posts( array( ‘post_type’=>’post’, ‘order’=>’ASC’, ‘orderby’=>’meta_value_num’, ‘meta_key’=>’date_event’, ‘posts_per_page’=> -1, ‘meta_query’ => array( ‘relation’ => ‘AND’, array( ‘key’ => ‘date_event’, ‘value’ … Read more

WP_Query and name__in

The parameter is not supported by WordPress core, but you can implement it using a filter on ‘posts_where’: // require PHP 5.3+ add_action( ‘posts_where’, function( $where, $query ) { if ( isset( $query->query[‘name__in’] ) && is_array( $query->query[‘name__in’] ) ) { global $wpdb; $names = array_filter( array_map( function( $name ) use( $wpdb ) { $sane = … Read more

Pagination custom query

This is a local/global variable problem. It can be solved either by changing the pagination function to work with local variable, or by promoting the local variable into global scope. As you are using wp_reset_postdata(), i guess you want to keep the original query. Change the pagination function to accept arguments – if ( ! … Read more

How to filter sql only for a specific post type

As @bonger said, let your filters accept a second argument, which will be the instance of WP_Query that’s executing the filter, and check it is indeed the main query for an event: function single_posts_fields( $sql, $wp_query ) { if ( $wp_query->is_main_query() && $wp_query->is_singular( ‘event’ ) ) { // Do your stuff } } add_filter( ‘posts_fields’, … Read more

Get_post() with meta_key when compare is a date

I think you have error in your query. Please try this: $posts = get_posts(array( ‘post_type’ => ‘events’, ‘posts_per_page’ => -1, ‘meta_query’ => array( ‘meta_key’ => ‘from_datetime’, ‘type’ => ‘DATETIME’, // You can also try changing it to TIME or DATE if it doesn’t work ‘meta_value’ => date( “F d, Y g:i a” ), ‘meta_compare’ => … Read more

Pagination shows 404 after a certain number of pages

The problem is that WordPress is executing the main query before your custom query (and the main query is based on the default post type only). You can intercept the main query, modify it, and then use it like so function add_blog_post_to_query( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( ‘post_type’, array(‘post’, … Read more

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