Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action('pre_get_posts','wpse50761_alter_query');
- function wpse50761_alter_query($query){
- if( $query->is_main_query() ){
- //Do something to main query
- }
- }
- if( have_posts() ):
- while( have_posts() ): the_post();
- //The loop
- endwhile;
- endif;
- $my_secondary_loop = new WP_Query(...);
- if( $my_secondary_loop->have_posts() ):
- while( $my_secondary_loop->have_posts() ): $my_secondary_loop->the_post();
- //The secondary loop
- endwhile;
- endif;
- wp_reset_postdata();
- <ul>
- <?php
- global $post;
- $args = array( 'numberposts' => 5, 'offset'=> 1, 'category' => 1 );
- $myposts = get_posts( $args );
- foreach( $myposts as $post ) : setup_postdata($post); ?>
- <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
- <?php endforeach; wp_reset_postdata(); ?>
- </ul>
- // ...
- global $wp_query;
- $wp_query = new WP_Query(array('your query vars here')); // sets the new custom query as a main query
- // your custom-post-type loop here
- wp_reset_query();
- // ...
- //unfortunately, "IS_PAGE" condition doesn't work in pre_get_posts (it's WORDPRESS behaviour)
- //so you can use `add_filter('posts_where', ....);` OR modify "PAGE" query directly into template file
- add_action( 'pre_get_posts', 'myf88' );
- function myf88($query) {
- if ( ! is_admin() && $query->is_main_query() ) {
- if ( $query->is_category ) {
- $query->set( 'post_type', array( 'post', 'page', 'my_postType' ) );
- add_filter( 'posts_where' , 'MyFilterFunction_1' ) && $GLOBALS['call_ok']=1;
- }
- }
- }
- function MyFilterFunction_1($where) {
- return (empty($GLOBALS['call_ok']) || !($GLOBALS['call_ok']=false) ? $where : $where . " AND ({$GLOBALS['wpdb']->posts}.post_name NOT LIKE 'Journal%')";
- }
- if( have_posts() ):
- while( have_posts() ): the_post();
- //The loop
- endwhile;
- endif;
- function have_posts() {
- global $wp_query;
- return $wp_query->have_posts();
- }
Add Comment
Please, Sign In to add comment