Guest User

Untitled

a guest
Oct 20th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. add_action('pre_get_posts','wpse50761_alter_query');
  2. function wpse50761_alter_query($query){
  3.  
  4. if( $query->is_main_query() ){
  5. //Do something to main query
  6. }
  7. }
  8.  
  9. if( have_posts() ):
  10. while( have_posts() ): the_post();
  11. //The loop
  12. endwhile;
  13. endif;
  14.  
  15. $my_secondary_loop = new WP_Query(...);
  16. if( $my_secondary_loop->have_posts() ):
  17. while( $my_secondary_loop->have_posts() ): $my_secondary_loop->the_post();
  18. //The secondary loop
  19. endwhile;
  20. endif;
  21. wp_reset_postdata();
  22.  
  23. <ul>
  24. <?php
  25. global $post;
  26. $args = array( 'numberposts' => 5, 'offset'=> 1, 'category' => 1 );
  27. $myposts = get_posts( $args );
  28. foreach( $myposts as $post ) : setup_postdata($post); ?>
  29. <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
  30. <?php endforeach; wp_reset_postdata(); ?>
  31. </ul>
  32.  
  33. // ...
  34.  
  35. global $wp_query;
  36. $wp_query = new WP_Query(array('your query vars here')); // sets the new custom query as a main query
  37.  
  38. // your custom-post-type loop here
  39.  
  40. wp_reset_query();
  41.  
  42. // ...
  43.  
  44. //unfortunately, "IS_PAGE" condition doesn't work in pre_get_posts (it's WORDPRESS behaviour)
  45. //so you can use `add_filter('posts_where', ....);` OR modify "PAGE" query directly into template file
  46.  
  47. add_action( 'pre_get_posts', 'myf88' );
  48. function myf88($query) {
  49. if ( ! is_admin() && $query->is_main_query() ) {
  50. if ( $query->is_category ) {
  51. $query->set( 'post_type', array( 'post', 'page', 'my_postType' ) );
  52. add_filter( 'posts_where' , 'MyFilterFunction_1' ) && $GLOBALS['call_ok']=1;
  53. }
  54. }
  55. }
  56. function MyFilterFunction_1($where) {
  57. return (empty($GLOBALS['call_ok']) || !($GLOBALS['call_ok']=false) ? $where : $where . " AND ({$GLOBALS['wpdb']->posts}.post_name NOT LIKE 'Journal%')";
  58. }
  59.  
  60. if( have_posts() ):
  61. while( have_posts() ): the_post();
  62. //The loop
  63. endwhile;
  64. endif;
  65.  
  66. function have_posts() {
  67. global $wp_query;
  68. return $wp_query->have_posts();
  69. }
Add Comment
Please, Sign In to add comment