פונקציית PRE_POST מאפשרת להשתלט על QUERY קיים ולהחליף את השאילתה, הקיימת מבלי שנית לגשת אלייה פיזית. הפונקציה הנ" עזרה לי בתבנית CUSTOMIZER לשלוט בQUERY של העמוד הראשי ולשנות את הערכים.
דוגמא1:
function gs_set_custom_query( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( 'posts_per_page','12' ); $query->set( 'cat', '226' ); } } add_action( 'pre_get_posts', 'gs_set_custom_query' );
דוגמא2:
function _additional_woo_query( $query ) { if ( is_product_category() ) { $query->set('posts_per_page', 20); } }
דוגמא3: שאילתה מתוך קטגוריה של קסטום פוסט טייפ CPT שומש באתר מטבחי יגל
add_filter( 'pre_get_posts', 'get_posts_plus_cpt_with_certain_tag' ); function get_posts_plus_cpt_with_certain_tag( $query ) { if ( $query->is_home() && empty( $query->query_vars['suppress_filters'] ) ) { if ( 1 == 1 ) { $taxquery = array( array( 'taxonomy' => 'portfolio_category', 'field' => 'ID', 'terms' => 51 //category id ) ); $query->set('tax_query', $taxquery); } $query->set( 'post_type', 'portfolio' ); } }