Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add conditions to existed WP_Query in Wordpress

Tags:

php

wordpress

I want to add some filter to select needed posts like:

function exclude_post($query) {
    if ($query->is_home) {

       // Do some PHP code

    }    
    return $query;
}

add_filter('pre_get_posts', 'exclude_post');

How can I add new conditions to existed WP_Query instance $query?

like image 278
Dmytro Zarezenko Avatar asked Mar 22 '23 14:03

Dmytro Zarezenko


1 Answers

If you want to modify your query with a filter, you can use $query->set('post_type', 'post'); in your function, just addapt with your parameter.

If you want to modify the main loop, you can use this :

global $wp_query;
$args = array_merge( $wp_query->query_vars, array( 'post_type' => 'product' ) );
query_posts( $args );
like image 90
Bonbelo Avatar answered Apr 02 '23 05:04

Bonbelo