Pages

Showing posts with label posts. Show all posts
Showing posts with label posts. Show all posts

Sunday, August 18, 2013

PHP-Convert a timestamp into facebook post time representation i.e years ago,months ago,weeks ago,days ago,hours ago,minutes ago,seconds ago format

This code is helpful for converting a timestamp field to a time stated as

years ago if it is more than an year ago
months ago if it is more than a month ago
week ago
days ago
hours ago
seconds ago

For example a post made yesterday will be shown as " 1 day ago"

Here goes the code
 <?php  
 $today = time();    
                 $createdday= strtotime($post['created']); //mysql timestamp of when post was created  
                 $datediff = abs($today - $createdday);  
                 $difftext="";  
                 $years = floor($datediff / (365*60*60*24));  
                 $months = floor(($datediff - $years * 365*60*60*24) / (30*60*60*24));  
                 $days = floor(($datediff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));  
                 $hours= floor($datediff/3600);  
                 $minutes= floor($datediff/60);  
                 $seconds= floor($datediff);  
                 //year checker  
                 if($difftext=="")  
                 {  
                   if($years>1)  
                    $difftext=$years." years ago";  
                   elseif($years==1)  
                    $difftext=$years." year ago";  
                 }  
                 //month checker  
                 if($difftext=="")  
                 {  
                    if($months>1)  
                    $difftext=$months." months ago";  
                    elseif($months==1)  
                    $difftext=$months." month ago";  
                 }  
                 //month checker  
                 if($difftext=="")  
                 {  
                    if($days>1)  
                    $difftext=$days." days ago";  
                    elseif($days==1)  
                    $difftext=$days." day ago";  
                 }  
                 //hour checker  
                 if($difftext=="")  
                 {  
                    if($hours>1)  
                    $difftext=$hours." hours ago";  
                    elseif($hours==1)  
                    $difftext=$hours." hour ago";  
                 }  
                 //minutes checker  
                 if($difftext=="")  
                 {  
                    if($minutes>1)  
                    $difftext=$minutes." minutes ago";  
                    elseif($minutes==1)  
                    $difftext=$minutes." minute ago";  
                 }  
                 //seconds checker  
                 if($difftext=="")  
                 {  
                    if($seconds>1)  
                    $difftext=$seconds." seconds ago";  
                    elseif($seconds==1)  
                    $difftext=$seconds." second ago";  
                 }  
                 echo " | ".$difftext;  
 ?>  

Thursday, November 8, 2012

Add Metaboxes and Metafields to your wordpress posts

Add Metaboxes and metafields to your wordpress posts


 add_action('add_meta_boxes','mypost_add_custom_box');  
 add_action('save_post','myplugin_save_postdata');  
 function mypost_add_custom_box() {  
   add_meta_box(  
     'myplugin_sectionid',  
     __('Post Type Selection','myplugin_textdomain'),  
     'myplugin_inner_custom_box',  
     'post'  
   );  
 }  
 function myplugin_inner_custom_box( $post ) {  
  wp_nonce_field( plugin_basename( __FILE__ ), 'myplugin_noncename' );  
  // The actual fields for data entry  
  $scheckval=esc_attr(get_post_meta($post->ID,'slider_post',true));  
  $pcheckval=esc_attr(get_post_meta($post->ID,'popular_post',true));  
  if($scheckval=='on')  
  echo '<input type="checkbox" id="myplugin_slider_field" name="myplugin_slider_field" checked="checked"/>';  
  else  
  echo '<input type="checkbox" id="myplugin_slider_field" name="myplugin_slider_field"/>';  
  echo '<label for="myplugin_slider_field">';  
     _e("Slider Post", 'myplugin_sdomain' );  
  echo '</label> ';  
  if($pcheckval=='on')  
  echo '<input type="checkbox" id="myplugin_popular_field" name="myplugin_popular_field" checked="checked"/>';  
  else  
  echo '<input type="checkbox" id="myplugin_popular_field" name="myplugin_popular_field"/>';  
  echo '<label for="myplugin_popular_field">';  
     _e("Popular Post", 'myplugin_pdomain' );  
  echo '</label>';  
 }  
 /* When the post is saved, saves our custom data */  
 function myplugin_save_postdata( $post_id ) {  
  // verify if this is an auto save routine.  
  // If it is our form has not been submitted, so we dont want to do anything  
  if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )  
    return;  
  // verify this came from the our screen and with proper authorization,  
  // because save_post can be triggered at other times  
  if ( !wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename( __FILE__ ) ) )  
    return;  
  // Check permissions  
  if ( 'page' == $_POST['post_type'] )  
  {  
   if ( !current_user_can( 'edit_page', $post_id ) )  
     return;  
  }  
  else  
  {  
   if ( !current_user_can( 'edit_post', $post_id ) )  
     return;  
  }  
  // OK, we're authenticated: we need to find and save the data  
  if($_POST['myplugin_slider_field']=='on')  
  $smydata=$_POST['myplugin_slider_field'];  
  else  
  $smydata='off';  
  if($_POST['myplugin_popular_field']=='on')  
  $pmydata=$_POST['myplugin_popular_field'];  
  else  
  $pmydata='off';  
  update_post_meta($post_id,'slider_post',$smydata);  
  update_post_meta($post_id,'popular_post',$pmydata);  
 }  

Easily get posts with a specific custom field/value on your WordPress blog

Easily get posts with a specific custom field/value on your WordPress blog

 <?php query_posts('meta_key=review_type&meta_value=movie'); ?>  
 <?php if (have_posts()) : ?>  
 <?php while (have_posts()) : the_post(); ?>  

Wordpress get posts on basis of name,category,tag

Since we have post name involved here long with category and tag we need to use custom query here which will provide us to search specific posts which contain special text in name and will belong to a particular category and will contain a tag as per requirements

Here is the custom query

I used a get in my search form with normal request parameters

 if (!(empty($_GET['section'])) || !(empty($_GET['article']))) {  
       global $wpdb, $post, $paged, $max_num_pages, $current_date;  
       $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;  
       $post_per_page = intval(get_query_var('posts_per_page'));  
       $offset = ($paged - 1) * $post_per_page;  
       $val = $_GET['s'];  
       $cat = $_GET['category'];  
       $tag = $_GET['tag'];  
 $querystr = "  
   SELECT DISTINCT $wpdb->posts.*  
   FROM $wpdb->posts  
   LEFT JOIN $wpdb->postmeta wpostmeta ON ($wpdb->posts.ID = wpostmeta.post_id)  
   LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)  
   LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)  
     WHERE  
     $wpdb->posts.post_type = 'post'  
     AND $wpdb->posts.post_status='publish'";  
 //Tags and categories were like dropdowns so each time either "0" for no select or tag_id or //category_id or both were sent via GET as url params  
       if($tag > 0 && $cat > 0)  
       {  
         $querystr = $querystr . " AND ($wpdb->posts.post_title like '%$val%' AND ($wpdb->term_taxonomy.taxonomy = 'post_tag'  
       AND $wpdb->term_taxonomy.term_id IN($tag))) OR ($wpdb->posts.post_title like '%$val%' AND ($wpdb->term_taxonomy.taxonomy = 'category'  
       AND $wpdb->term_taxonomy.term_id IN($cat)))";  
       }  
       elseif ($tag > 0) {  
         $querystr = $querystr ." AND $wpdb->posts.post_title like '%$val%' AND ($wpdb->term_taxonomy.taxonomy = 'post_tag'  
       AND $wpdb->term_taxonomy.term_id IN($tag))";  
       }  
       elseif ($cat > 0) {  
               $querystr = $querystr . " AND $wpdb->posts.post_title like '%$val%' AND ($wpdb->term_taxonomy.taxonomy = 'category'  
         AND $wpdb->term_taxonomy.term_id IN($cat))";  
          }  
       $pageposts = $wpdb->get_results($querystr, OBJECT);  
       $querystr = $querystr . " LIMIT " . $offset . ", " . $post_per_page . "; ";  
       $sql_posts_total = $wpdb->get_var("SELECT FOUND_ROWS();");  
       $max_num_pages = ceil($sql_posts_total / $post_per_page);  

 //And can use pagination as
  <?php if ($wp_query->max_num_pages > 1) : ?>  
           <nav id="<?php echo $nav_id; ?>">  
             <div class="nav-previous"><?php next_posts_link(__('<span class="meta-nav">&larr;</span> Older posts', 'old')); ?></div>  
             <div class="nav-next"><?php previous_posts_link(__('Newer posts <span class="meta-nav">&rarr;</span>', 'next')); ?></div>  
           </nav>  
   <?php endif; ?>   


This is one way out...

Other way out is to put this filter in your functions.php

 add_filter('posts_where','wpse_posts_where', 10, 2 );  
 function wpse_posts_where( $where, &$wp_query )  
 {  
   global $wpdb;  
   if ( $wpse_title = $wp_query->get( 'wpse_title' ) ) {  
     $where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . esc_sql( like_escape( $wpse_title ) ) . '%\'';  
   }  
   return $where;  
 }  


and then make a call to

 <?php if(!(empty($_GET['category'])) || !(empty($_GET['tag'])))  
 {  
   $val=$_GET['s'];  
   $cat=$_GET['category'];  
   $tag=$_GET['tag'];  
      global $query_string;  
      $qstr= $query_string."wpse_title=$val";  
      $args['wpse_title']=$val;  
      if($cat>0)  
      {  
           $args['category__and']=$cat;  
           $qstr=$qstr."&cat=$cat";  
      }  
      if ($tag>0)  
   {  
           $args['tag__in'] = $tag;  
           $qstr=$qstr."&tag=$tag";  
   }  
      query_posts($args);  
      query_posts($qstr);  
 ?>  


They both work how ever in later case youcan either make via name and category or name and tag

If you try to search via name category and tag you will end up getting a response which contains all the posts of that category..so I preffered using a custom query as it suited me