Advertisement
RUSSEL86

query code example

Feb 28th, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 19.27 KB | None | 0 0
  1. //Post Query in index.php
  2. <?php if(have_posts()) : ?>
  3. <?php while (have_posts()) : the_post(); ?>
  4. <--Your Post Query here -->
  5. <?php endwhile; ?>
  6. <?php endif; ?>
  7.         <!--or-->
  8. <?php if(have_posts()) : ?>
  9. <?php while (have_posts()) : the_post(); ?>
  10. <--Your Post Query here -->
  11. <?php endwhile; ?>
  12. <?php else : ?>
  13. <h3><?php _e('404 Error&#58; Not Found'); ?></h3>
  14. <?php endif; ?>
  15.  
  16.  
  17. //Custom page Query in page dynamic
  18. <?php while(have_posts()) : the_post(); ?>
  19.     <?php the_content(); ?>
  20. <?php endwhile; ?>
  21.  
  22. <?php while(have_posts()) : the_post(); ?>
  23.     <h2><?php the_title(); ?></h2>
  24. <?php endwhile; ?>
  25.  
  26. //Custom Post Query in index.php
  27. <?php
  28. global $post;
  29. $args = array( 'posts_per_page' => -1, 'post_type'=> 'working-method', 'department_cat' => 'Commerical', 'p' => '30', 'post__in' => array( 2, 5, 12, 14, 20 ) );
  30. $myposts = get_posts( $args );
  31. foreach( $myposts as $post ) : setup_postdata($post); ?>
  32. // Your Post Query here -->
  33. <?php endforeach; ?>
  34.  
  35. //Custom Post Query for pagenavi support
  36. <?php
  37.   $temp = $wp_query;
  38.   $wp_query = null;
  39.   $wp_query = new WP_Query();
  40.   $wp_query->query('showposts=6&category_name=Beauty&post_type=post'.'&paged='.$paged);
  41.   while ($wp_query->have_posts()) : $wp_query->the_post();
  42. ?>
  43.  
  44.  // Your Post Query here -->
  45.  
  46. <?php endwhile; ?>
  47. <?php if (function_exists('wp_pagenavi')) { wp_pagenavi(); } else { include('navigation.php'); } ?>
  48. <?php
  49.   $wp_query = null;
  50.   $wp_query = $temp;  // Reset
  51. ?>
  52.      
  53. ----------------------or --------------------------
  54. // Post Query for pagenavi support
  55. <?php query_posts('post_type=post&post_status=publish&posts_per_page=30&paged='. get_query_var('paged')); ?>
  56.     <?php if(have_posts()) : ?>
  57.     <?php while (have_posts()) : the_post(); ?>
  58.         // Your Post Query here -->
  59.     <?php endwhile; ?> 
  60.     <?php endif; ?>
  61. <?php if (function_exists('wp_pagenavi')) { wp_pagenavi(); } else { include('navigation.php'); } ?>    
  62.  
  63.  
  64. ==============================================
  65. QUERY CODE FOR RELATED POST WITH AND WITHOUT THUMBNAIL
  66. ==============================================
  67. <?php $orig_post = $post;
  68.     global $post;
  69.     $categories = get_the_category($post->ID);
  70.     if ($categories) {
  71.     $category_ids = array();
  72.     foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
  73.  
  74.     $args=array(
  75.     'category__in' => $category_ids,
  76.     'post__not_in' => array($post->ID),
  77.     'posts_per_page'=> 6, // Number of related posts that will be shown.
  78.     'caller_get_posts'=>1
  79.     );
  80.  
  81.     $my_query = new wp_query( $args );
  82.     if( $my_query->have_posts() ) {
  83.     echo '<div class="single_coment_all">';
  84.     while( $my_query->have_posts() ) {
  85.     $my_query->the_post();?>
  86.     <div class="single_coment floatleft fix">
  87.         <div class="relatedthumb_img">
  88.             <a href="<?php the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>">
  89.                 <?php the_post_thumbnail('related_image'); ?>
  90.             </a>
  91.         </div>
  92.         <div class="comments_caption">
  93.             <div class="comments_caption_left">
  94.                 <p><?php the_category(', '); ?></p>
  95.             </div>
  96.             <div class="comments_caption_right">
  97.                 <a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>
  98.             </div>
  99.         </div>
  100.     </div>
  101.     <?php
  102.     }
  103.     echo '</div>';
  104.     }
  105.     }
  106.     $post = $orig_post;
  107.     wp_reset_query(); ?>
  108.  
  109. =============================================
  110. কোন একটা নির্দিষ্ট ক্যাটেগরির পপুলার পোস্ট শো করুন লিস্ট আকারে
  111. =============================================  
  112. <?php
  113.  
  114. $args=array(
  115. 'cat' => 58, // this is category ID
  116. 'orderby' => 'comment_count',
  117. 'order' => 'DESC',
  118. 'post_type' => 'post',
  119. 'post_status' => 'publish',
  120. 'posts_per_page' => 6, // how much post you want to display
  121. 'caller_get_posts'=> 1
  122. );
  123.  
  124. $my_query = null;
  125. $my_query = new WP_Query($args);
  126. if( $my_query->have_posts() ) { ?>
  127.  
  128. <ul>
  129. <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
  130.  
  131. <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
  132.  
  133. <?php  endwhile; ?>
  134. </ul>
  135. <?php }
  136.  
  137. wp_reset_query(); ?>
  138.  
  139. ========================================
  140. show related posts form current taxonomy
  141. ==========================================
  142.  
  143. function pippin_related_posts($taxonomy = '') {
  144.    
  145.     global $post;
  146.    
  147.     if($taxonomy == '') { $taxonomy = 'post_tag'; }
  148.    
  149.     $tags = wp_get_post_terms($post->ID, $taxonomy);
  150.     $content = '';
  151.     if ($tags) {
  152.         $first_tag  = $tags[0]->term_id;
  153.         $second_tag = $tags[1]->term_id;
  154.         $third_tag  = $tags[2]->term_id;
  155.         $args = array(
  156.             'post_type' => get_post_type($post->ID),
  157.             'posts_per_page' => 4,
  158.             'tax_query' => array(
  159.                 'relation' => 'OR',
  160.                 array(
  161.                     'taxonomy' => $taxonomy,
  162.                     'terms' => $second_tag,
  163.                     'field' => 'id',
  164.                     'operator' => 'IN',
  165.                 ),
  166.                 array(
  167.                     'taxonomy' => $taxonomy,
  168.                     'terms' => $first_tag,
  169.                     'field' => 'id',
  170.                     'operator' => 'IN',
  171.                 ),
  172.                 array(
  173.                     'taxonomy' => $taxonomy,
  174.                     'terms' => $third_tag,
  175.                     'field' => 'id',
  176.                     'operator' => 'IN',
  177.                 )
  178.             )
  179.         );
  180.         $related = get_posts($args);
  181.         $i = 0;
  182.         if( $related ) {
  183.             global $post;
  184.             $temp_post = $post;
  185.                 foreach($related as $post) : setup_postdata($post);
  186.                     $content .= '<ul class="related-posts-box">';
  187.                         $content .= '<li><a href="' . get_permalink() . '" title="' . get_the_title() . '">' . get_the_title() . '</a></li>';
  188.                     $content .= '</ul>';
  189.                    
  190.                 endforeach;
  191.             $post = $temp_post;
  192.         }
  193.     }
  194.  
  195.     return $content;
  196. }        
  197.  
  198.  
  199. add_action('the_content', 'do_jt_related_posts');
  200. function do_jt_related_posts() {
  201.  
  202.     if( is_singular('post') ) :
  203.         echo get_the_content();
  204.         echo pippin_related_posts();       
  205.     else :
  206.         echo get_the_content();
  207. endif;  }
  208.  
  209. ==============================================
  210. query code for display post from each category
  211. ==============================================
  212. <?php
  213.   // array of category IDs
  214.   $categories =  array(10,9,8,7,6,5,4,3,2,1);
  215.  
  216.   foreach ($categories as $cat) :
  217.     $post = false;
  218.     $post = get_posts('cat='.$cat.'&posts_per_page=1');
  219.     if($post) :
  220.       $post = $post[0];
  221.       setup_postdata($post); ?>
  222.     // Your content for loop
  223.  
  224.     <?php endif; ?>
  225.   <?php endforeach; ?>
  226.  
  227.  
  228. ==================================================
  229.         bootstrap carousel query code
  230. ==================================================
  231.   <!-- Carousel-->
  232. <?php
  233. $items = new WP_Query(array(
  234. 'post_type' => 'banner-items',
  235. 'posts_per_page' => -1,
  236. 'meta_key' => '_thumbnail_id'
  237. ));
  238. $count = $items->found_posts;
  239. ?> 
  240.    
  241.   <div id="myCarousel" class="carousel slide" data-ride="carousel">
  242.       <!-- Indicators -->
  243.     <ol class="carousel-indicators">
  244.       <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
  245.       <?php for($num = 1; $num < $count; $num++){ ?>
  246.       <li data-target="#myCarousel" data-slide-to="<?php echo $num; ?>"></li>
  247.       <?php } ?>
  248.     </ol>
  249.  
  250.     <div class="carousel-inner">
  251. <?php
  252.         $ctr = 0;
  253.         while ( $items->have_posts() ) :
  254.           $items->the_post();
  255.           $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
  256.           $custom = get_post_custom($post->ID);
  257.           $link = $custom["more-link"][0];
  258.           $class = $ctr == 0 ? ' active' : '';
  259.         ?>
  260.    
  261.         <div class="item<?php echo $class; ?>" id="<? the_ID(); ?>">
  262.             <img src="<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), '' ); echo $image[0]; ?>">
  263.             <div class="container">
  264.               <?php the_content(); ?>
  265.             </div>
  266.         </div>
  267.     <?php $ctr++;
  268.         endwhile;  ?>
  269.      
  270.      
  271.     </div>
  272.     <!-- /.carousel-inner -->
  273.    
  274.     <!-- Controls -->
  275.     <a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev"> <span class="fa fa-angle-left fa-5x"></span> </a>
  276.     <a class="right carousel-control" href="#myCarousel" role="button" data-slide="next"> <span class="fa fa-angle-right fa-5x"></span> </a>
  277.    
  278.     </div>
  279.   <!-- /.carousel -->
  280.  
  281. ============================================
  282.  Carousel Loop for slide 4 or many post together
  283. ============================================
  284. function event_get_carousel(){
  285.  $owlcarousel= '<div class="single_events">';
  286.  $efs_query= "post_type=tribe_events&posts_per_page=-1";
  287.  query_posts($efs_query);
  288.  if (have_posts()) : while (have_posts()) : the_post();
  289.   $thumb= get_the_post_thumbnail();
  290.   $owlcarousel.=' <div class="single_ticket">  
  291.       '.$thumb.'
  292.       <h2>'.get_the_title().'</h2>
  293.       <p>' . tribe_get_full_address( get_the_ID() ) . '</p>
  294.       <h3>'.tribe_events_event_schedule_details().'</h3>
  295.       <a href="'.get_the_permalink().'">Buy Ticket</a>
  296.       </div>
  297.      ';  
  298.  endwhile; endif; wp_reset_query();
  299.  $owlcarousel.= '</div>';
  300.  return $owlcarousel;
  301. }
  302.  
  303. /**add the shortcode for the slider- for use in editor**/
  304. function event_insert_carousel($atts, $content=null){
  305.  $owlcarousel= event_get_carousel();
  306.  return $owlcarousel;
  307. }
  308. add_shortcode('events', 'event_insert_carousel');
  309.  
  310.  
  311. ==================================================
  312. How to remove menus in WordPress dashboard
  313. ==================================================
  314. Simply paste the following code into the functions.php file of your theme. The following example will remove all menus named in the $restricted array.
  315.  
  316. function remove_menus () {
  317. global $menu;
  318.     $restricted = array(__('Dashboard'), __('Posts'), __('Media'), __('Links'), __('Pages'), __('Appearance'), __('Tools'), __('Users'), __('Settings'), __('Comments'), __('Plugins'));
  319.     end ($menu);
  320.     while (prev($menu)){
  321.         $value = explode(' ',$menu[key($menu)][0]);
  322.         if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}
  323.     }
  324. }
  325. add_action('admin_menu', 'remove_menus');
  326.  
  327.  =======================================================
  328. bxslider.com/examples/thumbnail-pager-1   dynamic code by custom post
  329. =======================================================
  330. <div id="slider" class="row" style="padding:0; position:relative">
  331.     <ul class="bxslider" >
  332.         <?php  $args = array(
  333.             'post_type' => 'slider',
  334.             'posts_per_page' => 4,
  335.             'order' => 'DESC',
  336.             'orderby' => 'date'
  337.         );?>
  338.  
  339.         <?php $slider = new WP_Query($args); while($slider->have_posts()) : $slider->the_post(); ?>
  340.       <li>
  341.         <?php the_post_thumbnail('slider'); ?>
  342.         <div class="col-md-7 hidden-sm hidden-xs" style="padding:0">
  343.             <div class="titulo">
  344.                 <h3><?php the_title(); ?></h3>
  345.             </div>
  346.         </div>
  347.       </li>
  348.  
  349.     <?php endwhile; wp_reset_postdata(); ?>
  350.     </ul>
  351.  
  352.     <div class="container navegador hidden-sm hidden-xs">
  353.         <div class="row">
  354.             <div class="col-md-5 col-md-offset-7 text-right" style="padding:0;" >
  355.                 <div id="bx-pager">
  356.                     <?php  $args = array(
  357.                         'post_type' => 'slider',
  358.                         'posts_per_page' => 4,
  359.                         'order' => 'DESC',
  360.                         'orderby' => 'date'
  361.                     );?>
  362.                     <?php $numero = 0; ?>
  363.                         <?php $slider = new WP_Query($args); while($slider->have_posts()) : $slider->the_post(); ?>
  364.                             <a data-slide-index="<?php echo $numero; ?>" href=""><?php the_post_thumbnail('thumbnailSlider'); ?></a>
  365.                         <?php $numero++; ?>
  366.                     <?php endwhile; wp_reset_postdata(); ?>
  367.                 </div>
  368.             </div>
  369.         </div>
  370.     </div>
  371.  
  372.     <div class="clearfix"></div>
  373.  
  374. </div> <!--/#slider -->
  375.  
  376. =======================================================
  377. Owl carousel dynamic code multiple item like 4 item slide with 1 item
  378. =======================================================
  379. <div class="custom_slider">
  380.     <div id="owl-demo" class="owl-carousel owl-theme">
  381.        
  382.         <?php if(!is_paged()) { ?>
  383.         <?php
  384.         $args = array( 'post_type' => 'main-slider', 'posts_per_page' => -1 );
  385.         $loop = new WP_Query( $args );
  386.         $count=2;
  387.         ?>
  388.  
  389.         <div class="item">
  390.         <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
  391.        
  392.         <?php
  393.                 $state = get_post_meta($post->ID, 'state', true);
  394.                 $location = get_post_meta($post->ID, 'location', true);
  395.                 $phone = get_post_meta($post->ID, 'phone', true);
  396.                 $website = get_post_meta($post->ID, 'website', true);
  397.                 $facebook = get_post_meta($post->ID, 'facebook', true);
  398.                 $fblink = get_post_meta($post->ID, 'fblink', true);
  399.                
  400.             ?>
  401.        
  402.        
  403.             <div class="single_slider floatleft">
  404.                 <div class="fetured_img">
  405.                     <a href="<?php echo site_url(); ?>/EPARCH "><?php the_post_thumbnail('main_slider_image'); ?></a>
  406.                 </div>
  407.                 <a href="<?php echo site_url(); ?>/EPARCH "><h2><?php the_title();?></h2></a>
  408.                 <p><?php echo $state; ?> <?php echo $location; ?><br/>
  409.                 <?php echo $phone; ?> <span><?php echo $website; ?></span></p>
  410.                 <div class="social_icon">
  411.                     <a href="<?php echo $fblink; ?>"><img src="<?php echo $facebook; ?>" alt="" /></a>
  412.                 </div>
  413.             </div>
  414.         <?php
  415.         if($count%4==1&&$count>1){
  416.             echo '</div><div class="item">';
  417.             }
  418.             $count++;
  419.          ?>
  420.         <?php endwhile; ?>
  421.         </div>
  422.         <?php wp_reset_query(); ?>
  423.         <?php } ?>
  424.            
  425.     </div>
  426. </div>
  427.  
  428.  
  429. =======================================================
  430. bxslider.com/examples/thumbnail-pager-1   dynamic code by Metabox slider
  431. =======================================================
  432. <div class="portfolio_single_wrapper fix">
  433.                
  434.                 <?php
  435.                 $spyr_demo_slider = get_post_meta( $post->ID, 'spyr_demo_slider', true );
  436.                
  437.                 echo '<div id="portfolio_single_slider" class="fix">';
  438.                 foreach( $spyr_demo_slider as $slide ) {
  439.                            
  440.                             echo '<div class="portfolio_single_item fix">
  441.                             <img src="'. $slide['image'] .'" alt="" />
  442.                             </div>';
  443.                 }
  444.                 echo '</div>';
  445.  
  446.                 ?>
  447.                
  448.                
  449.             <?php
  450.                 $spyr_demo_slider = get_post_meta( $post->ID, 'spyr_demo_slider', true );
  451.                 $numero = 0;
  452.                
  453.                 echo '<div id="portfolio_single_pager" class="fix">';
  454.                 foreach( $spyr_demo_slider as $slide ) {
  455.                    
  456.                            
  457.                             echo '<a data-slide-index="'.$numero.'" class="pager" href=""><img src="'. $slide['image'] .'" alt="" /></a>';
  458.                     $numero++;
  459.                 }
  460.                
  461.                 echo '</div>';
  462.                
  463.                 ?>
  464.                 <div class="portfolio_single_content fix">
  465.                     <h2><?php the_title(); ?></h2>
  466.                     <?php the_content(); ?>
  467.                 </div>
  468.             </div>
  469.             <!-- Portfolio Single Page Content End There -->
  470.            
  471. ===========================================================
  472. conditional query code for different post size by one query
  473. ===========================================================
  474. <?php
  475. $the_query = new WP_Query( array( 'posts_per_page' => 7 ) );
  476. if ( $the_query->have_posts()) : ?>
  477. <?php $count = 0; ?>
  478. <?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
  479. <?php $count++; ?>
  480.  
  481.  
  482.   <?php if ($count == 1) : ?>
  483.  
  484.     <div class="third_post single-latest">
  485.        <a href="<?php the_permalink(); ?>"> <?php the_title(); ?></a>
  486.         <?php the_post_thumbnail('post-image'); ?>
  487.         <?php the_excerpt(); ?>
  488.     </div>  
  489.    
  490.   <?php elseif ($count == 2) : ?>
  491.     <div class="fourth_post single-latest">      
  492.              
  493.         <a href="<?php the_permalink(); ?>"> <?php the_title(); ?></a>
  494.         <?php the_post_thumbnail('post-image'); ?>
  495.         <?php the_excerpt(); ?>
  496.     </div>
  497.   <?php elseif ($count == 3) : ?>
  498.     <div class="fourth_post single-latest">      
  499.              
  500.         <a href="<?php the_permalink(); ?>"> <?php the_title(); ?></a>
  501.         <?php the_post_thumbnail('post-image'); ?>
  502.         <?php the_excerpt(); ?>
  503.     </div>
  504.    
  505.    <?php else : ?>
  506.     <div class="single_post single-latest">
  507.          <a href="<?php the_permalink(); ?>"> <?php the_title(); ?></a>        
  508.           <?php the_excerpt(); ?>
  509.     </div>
  510.    
  511. <?php endif; ?>
  512. <?php endwhile; ?>
  513. <?php endif; ?>
  514.  
  515.  
  516.  
  517. ===========================
  518. কাস্টম পোষ্ট টাইপ থেকে ৩টা ইভেন্ট এনে শো করাতে হবে, কিন্তু সমস্যা হল "featured" category কে প্রাধান্য দিতে হবে। "featured" category থেকে যদি ৩টা আপকামিং ইভেন্ট পেয়ে যাই তাহলে শো করবে, যদি "featured" category থেকে যদি ৩টা আপকামিং ইভেন্ট না পাই তখন অন্য category থেকে আপকামিং ইভেন্ট এনে শো করাতে হবে। যেমন- "featured" category থেকে ১টা আপকামিং ইভেন্ট পাইলে বাকি ২টা আপকামিং ইভেন্ট অন্য category থেকে এনে শো করবে।
  519. ===========================
  520. $ev1 = [];
  521.         $ev2 = [];
  522.  
  523.         $events1_args = array(
  524.           'post_type'     => 'ctc_event',                
  525.           'meta_query' => array(
  526.             array(
  527.               'key' => '_ctc_event_end_date',
  528.               'value' => date_i18n( 'Y-m-d' ),
  529.               'compare' => '>=',
  530.               'type' => 'DATE'
  531.             ),
  532.           ),
  533.           'meta_key'      => '_ctc_event_start_date_start_time',
  534.           'meta_type'     => 'DATETIME',
  535.           'orderby'     => 'meta_value',
  536.           'order'       => 'ASC',
  537.           'posts_per_page' => 3,
  538.           'fields' => 'ids',
  539.           'tax_query' => array(
  540.               array(
  541.                   'taxonomy' => 'ctc_event_category',
  542.                   'field'    => 'slug',
  543.                   'terms'    => 'featured',
  544.               ),
  545.           ),
  546.         );
  547.  
  548.         $events1 = new WP_Query( $events1_args );
  549.         $e1count = $events1->post_count;
  550.         $ev1 = $events1->posts;
  551.  
  552.         if($e1count < 3){
  553.             $events2_args = array(
  554.             'post_type'     => 'ctc_event',                
  555.             'meta_query' => array(
  556.               array(
  557.                 'key' => '_ctc_event_end_date',
  558.                 'value' => date_i18n( 'Y-m-d' ),
  559.                 'compare' => '>=',
  560.                 'type' => 'DATE'
  561.               ),
  562.             ),
  563.             'meta_key'      => '_ctc_event_start_date_start_time',
  564.             'meta_type'     => 'DATETIME',
  565.             'orderby'     => 'meta_value',
  566.             'order'       => 'ASC',
  567.             'posts_per_page' => (3 - $e1count),
  568.             'fields' => 'ids',
  569.             'tax_query' => array(
  570.                 array(
  571.                     'taxonomy' => 'ctc_event_category',
  572.                     'field'    => 'slug',
  573.                     'terms'    => 'featured',
  574.                     'operator' => 'NOT IN',
  575.                 ),
  576.             ),
  577.           );
  578.  
  579.           $events2 = new WP_Query( $events2_args );
  580.           $ev2 = $events2->posts;
  581.         }        
  582.  
  583.         $event_ids = array_merge( $ev1, $ev2);
  584.         $events = new WP_Query(array(
  585.             'post_type' => 'ctc_event',
  586.             'post__in'  => $event_ids,
  587.             'meta_query' => array(
  588.                 array(
  589.                   'key' => '_ctc_event_end_date',
  590.                   'value' => date_i18n( 'Y-m-d' ),
  591.                   'compare' => '>=',
  592.                   'type' => 'DATE'
  593.                 ),
  594.               ),
  595.               'meta_key'      => '_ctc_event_start_date_start_time',
  596.               'meta_type'     => 'DATETIME',
  597.               'orderby'     => 'meta_value',
  598.               'order'       => 'ASC',
  599.         ));
  600.  
  601.         if ($events->have_posts()) :  
  602.           while ( $events->have_posts() ) : $events->the_post();  
  603.             // HTML here
  604.           endwhile;
  605.         endif;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement