Tag Archives: wordpress
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

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 }
Links

bbPress 2.0 beta 3 is available

If you’ve been keeping up-to-date with news regarding bbPress 2.0 (aka the plugin version of the bbPress forum software), then you’ll be glad to hear John James Jacoby just released beta 3 which can be downloaded now from the WordPress plugin directory.

∞ Visit Link: http://wpcandy.com/reports/bbpress-2-0-beta-3-is-a...

Comments { 0 }
Video

Video: Scaling WordPress for High-Traffic

This video, originally presented by Envato’s Ryan Allen at WordCamp Melbourne is a pretty good listen for learning what your best options are if your wordpress site get hit with massive traffic spikes.

At Envato, Ryan is reponsible for managing (server) performance at some of Australia’s busiest websites.

The Envato marketplace network (ActiveDen, ThemeForest, CodeCanyon, AudioJungle, 3dOcean, VideoHive, GraphicRiver and the Tuts+ Marketplace) reached a considerable milestone in October 2010 of a combined membership of 500,000 members around the world, more than double its membership of just one year ago.

Managing traffic spikes is crucial as projects get bigger and Ryan will will speaking about what to do when that happens to your WordPress site.

If your website gets hit by massive trafic flows what are your best options?

WARNING: SOME LANGUAGE MAY OFFEND, It should also be noted that Ryan has a unique (but hilarious) sense of humor).

Comments { 0 }
Quick Function: Check for parent category in wordpress Code

Quick Function: Check for parent category in wordpress

When I was working on foodjumper, there were times when I wanted to check the parent category, for example, if you’re in the Dinner category or viewing a post in that category, I want to display the proper layout for posts in the Recipes category.

One function that works, is to do a check like this:


function parent_category( $cats, $_post = null ){
     if( is_string($cats) )   $cats = get_cat_ID($cats);
     foreach ( (array) $cats as $cat ) {
          $descendants = get_term_children( (int) $cat, 'category');
          if ( $descendants && in_category( $descendants, $_id ) ) return true;
     }
     return false;
}

WordPress published a similar function on their site, but it used the actual category ID in the function. I found it easier to let you enter the slug instead.

Now, when you want to check if a post is in a category that is a child, you can do a check like this:


<?php
if(in_category('recipes') || parent_category('recipes') ):
     // display this post as a recipe
else:
     // display this post as a normal post
endif;
?>

This can be adapted to almost any type of similar treatment, I just used recipes above as an example since it’s where I just used this function.

Comments { 3 }
Page 1 of 41234