Wordpress - How can I remove year condition from main sql query?
I want to customize the main query of wordpress for my custom post type.
Here is how I did it:
function wpa_date_archive_post_types( $query ){
if( $query->is_main_query() && $query->is_date() ):
$query->set( 'post_type' , array( 'movie' ) );
$query->set( 'tag' , '1994' );//For testing pupose I put a
hard coded value...
$query->set( 'post_status' , array( 'publish' ) );
endif;
}
add_action( 'pre_get_posts', 'wpa_date_archive_post_types' );
WP generates the following query:
SELECT
SQL_CALC_FOUND_ROWS wp_posts.*
FROM
wp_posts
INNER JOIN
wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
WHERE
1=1
AND
YEAR(wp_posts.post_date)='1994'--WP gets 1994 here from an URL
parameter (year)
AND
(wp_term_relationships.term_taxonomy_id IN (24) )
AND
wp_posts.post_type IN ('movie')
AND
(wp_posts.post_status = 'publish')
GROUP BY
wp_posts.ID
ORDER BY
wp_posts.post_date DESC
LIMIT
0, 5
I want to remove the following line from the final query:
AND
YEAR(wp_posts.post_date)='1994'
I tried to add this PHP code
// ...
if( $query->is_main_query() && $query->is_date() ):
$query->set( 'post_type' , array( 'movie' ) );
$query->set( 'year' , 0 );
$query->set( 'tag' , '1994' );
$query->set( 'post_status' , array( 'publish' ) );
endif;
// ...
But I get a blank page. How can I disable year from sql query ?
No comments:
Post a Comment