Tumblogs Archives: Code
Code

WordPress: Search By Post and Meta Fields

If you’ve ever had to search custom post types, this function will come in handy:


<?php
  function search_by_post_and_meta_fields($args) {
    global $wpdb;
    $defaults = array(
      'post_type' => 'post',
      'post_fields_to_search' => false,
      'meta_fields_to search' => false,
      'search_term' => false
    );
    extract(wp_parse_args($args, $defaults));
    if(($post_fields_to_search or $meta_fields_to_search) and $search_term) {
      $ids = array();
      $query = "SELECT DISTINCT $wpdb->posts.ID
      FROM $wpdb->posts
      LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
      WHERE ($wpdb->posts.post_status = 'publish'
      AND $wpdb->posts.post_type = '$post_type') AND (";
       $where_parts = array();
      if(is_array($post_fields_to_search)) {
        foreach($post_fields_to_search as $field) {
          $where_parts[] = "$wpdb->posts.$field LIKE '%$search_term%'";
        }
      }
      if(is_array($meta_fields_to_search)) {
        foreach($meta_fields_to_search as $field) {
          $where_parts[] = "( $wpdb->postmeta.meta_key = '$field' AND $wpdb->postmeta.meta_value LIKE '%$search_term%' )";
        }
      }
      if(count($where_parts)) $query .= implode(' OR ', $where_parts);
      $query .= ") ORDER BY $wpdb->posts.ID ASC";
      $results = $wpdb->get_results($query, ARRAY_A);
      foreach($results as $result){
        $ids[] = $result['ID'];
      }
      return $ids;
    }
    return false;
  }
?>

Then in your template, you just call it like follows:


<?php
$args = array(
  'post_type' => 'listing',
  'post_fields_to_search' => array('post_title', 'post_content'),
  'meta_fields_to_search' => array('_tags'),
  'search_term' => 'hello'
);
$post_ids = search_by_post_and_meta_fields($args);
foreach($post_ids as $post_id) {
  $post = get_post($post_id);
  setup_postdata($post);
}
?>
Comments { 0 }
Code

Quick Function: retrieve your call recordings from Twilio

A recent client project lead me to an interesting problem, normally for retrieving a call recording in twilio, you would store info afterwards, but some calls, that’s not an option. If it’s a recording between two people, like say, a customer and a sales person to review later, you simply set:


 <dial record=true>phone number</dial>

There’s no option there to tell it to forward to another URL afterwards, and the tests that I tried using the action argument ended up failing.

So here we were, we had the SID for the call, but no recording.

Solution? Build a quick function to search the recordings based on call SID using the api…

My only problem, the twilio php api doesn’t include an easy way to get recordings, it lets grab call logs, but that’s it.

So my answer was to write this function:
(more…)

Comments { 0 }
Code

Some handy WordPress multisite functions

Quite often when working with WordPress, I end up working with the multisite functionality, and usually the client (which can be myself) wants to show posts on the main site from across the various blogs.

These functions make that possible, mainly the main function “multisite_latest_posts” makes that possible.

The name isn’t entirely true, yes it returns the latest posts, but you can control the sorting, the amount of posts returned, add some pagination and thanks to the recent addition of meta table queries to the function, return only what you need. But I’ve use this function for a while and didn’t want to totally rename it:

To start, I usually create a file in my theme called “multisite_functions.php”, or I copy the code into functions.php, your choice. But if you choose to create the file, then remember to include it in your functions.php file like so:


include("multisite_functions.php");

Again, there’s also no reason not to just copy these functions directly into your functions.php file if you choose.

Anyhow, let’s get onto the code.
(more…)

∞ Visit Link: https://gist.github.com/1092505

Comments { 0 }
Code

WordPress Function: Deregister widgets that won’t be used.

WordPress’s widgets are handy, they can simplify and also extend your site. But sometimes, you may have other widgets you want to use (for example, if you’ve built a theme that has it’s own custom widgets), or if you’re setting up a client site that uses widgets, maybe you want them to focus on specific widgets instead, so I like to add this to the theme’s functions.php file and simply remove any widgets from the list that you want to allow, or add any that you want to disallow that aren’t already on the list:


function unregister_default_wp_widgets() {
   unregister_widget('WP_Widget_Pages');
   unregister_widget('WP_Widget_Calendar');
   unregister_widget('WP_Widget_Archives');
   unregister_widget('WP_Widget_Links');
   unregister_widget('WP_Widget_Meta');
   unregister_widget('WP_Widget_Search');
   unregister_widget('WP_Widget_Categories');
   unregister_widget('WP_Widget_Recent_Posts');
   unregister_widget('WP_Widget_Recent_Comments');
   unregister_widget('WP_Widget_Tag_Cloud');
   unregister_widget('WP_Widget_RSS');
   unregister_widget('WP_Widget_Akismet');
}
add_action('widgets_init', 'unregister_default_wp_widgets', 1);

The end result, is a cleaner widget page so your users can focus exactly on the widgets you want them to use.

Comments { 1 }
Code

Sanitizing PHP request variables quickly


<?php
foreach($_POST as $k=>$v){
    $_POST[$k] = filter_input(INPUT_GET, $k, FILTER_SANITIZE_SPECIAL_CHARS);
}
foreach($_GET as $k=>$v){
    $_GET[$k] = filter_input(INPUT_GET, $k, FILTER_SANITIZE_SPECIAL_CHARS);
}
?>
Comments { 0 }
Page 1 of 41234