meta_query compare operator explanation

=   equals
!=  does not equal
>   greater than
>=  greater than or equal to
<   less than
<=  less than or equal to

LIKE and NOT LIKE

LIKE and NOT LIKE are SQL operators that let you add in wild-card symbols, so you could have a meta query that looks like this:

array( 
    'key' => 'name', 
    'value' => 'Pat', 
    'compare' => 'LIKE'
)

This would return all posts where the meta value “name” has the string “Pat”. In this case, “Pat” “Patricia” and “Patrick” would all be returned back to you. There’s a non-WordPress tutorial explanation here.

Adding the wildcard character % isn’t necessary, because it gets added by default like @Herb said in his below answer. Like this: $meta_value = '%' . like_escape( $meta_value ) . '%'; – see source.


IN and NOT IN

IN and NOT IN select any matches that are in (or not in) the given array. So you could do something like this:

array(
    'key'     => 'color', 
    'value'   => array('red', 'green', 'blue') 
    'compare' => 'IN'
)

and it would get all posts that have the color set to either red, green, or blue. Using ‘NOT IN’ gets the reverse, any posts that have a value set to anything else than what’s in the array.

The generated SQL for this would look something like this:

SELECT * FROM posts_meta WHERE value IN ("red", "green", "blue") 

BETWEEN and NOT BETWEEN

BETWEEN and NOT BETWEEN allow you to define a range of values that could be correct, and require you to give two values in an array in your meta_query:

array( 
    'key' => 'price', 
    'value' => array(20,30) 
    'compare' => 'BETWEEN'
)

This will get you all posts where the price is between 20 and 30. This person digs into an example with dates.


NOT EXISTS

NOT EXISTS is just like what it sounds – the meta value isn’t set or is set to a null value. All you need for that query is the key and comparison operator:

array( 
    'key' => 'price', 
    'compare' => 'NOT EXISTS'
)

This person needed to query non-existent meta values, and needed them to play nice with others.

Copy a file content to a gz file

Here’s a code to create and write the content from a url to a gz file.

In this example, we are copying the content of an rss feed to a gz file. RSS Feed is usually an xml file that’s why we are adding .xml before the .gz extension.

$content = 'http://example.com/rss/feed';
$gz = gzopen ( 'filename.xml.gz', 'w9' );
gzwrite ( $gz, $content );
gzclose ( $gz ); 

Workaround in getting image details that returns a 403 error.

If you are trying to pull data that returns a 403 error then here’s the workaround for that. Use stream_context_create and use the result as a parameter in the file_get_contents.

In this example, it is trying to pull the image size of the image from the $url variable.

$ctx = stream_context_create(array(
        'http' => array('timeout' => 25, 'protocol_version' => 1.1,'user_agent' => 'My User Agent/1.0'),
        'https' => array('timeout' => 25, 'protocol_version' => 1.1,'user_agent' => 'My User Agent/1.0')
    ));

$dimension = getimagesizefromstring(  file_get_contents($url, 0, $ctx) );

Shorten your link using bitly API

Below is the code on how to make use of bitly API but first things first. In order to use the code below, you must create an account in bitly to gain your login and API Key which are both required in the API call. Once you have that setup, copy and paste the code below and you’re ready to get rollin’.

function makeBitlyUrl($url, $login, $apiKey, $format = 'xml', $version = '2.0.1')
{
	//First, you need to create the URL
	$bitly = 'http://api.bit.ly/shorten?version='.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$apiKey.'&format='.$format;
	
	//Then, get the return data from the API call
	$response = file_get_contents($bitly);
	
	//Parse data depending on your preferred format
	if( strtolower( $format ) == 'json' )
	{
		$json = @json_decode($response,true);
		return $json['results'][$url]['shortUrl'];
	}
	else //Default is XML
	{
		$xml = simplexml_load_string($response);
		return 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
	}
}

$shortenedUrl = makeBitlyUrl('https://example.com', 'winnie', 'R_123456789abcde', 'json');

PHP: How to detect the device used by a user?

iOS
if (stripos($_SERVER['HTTP_USER_AGENT'],"iPod") || stripos($_SERVER['HTTP_USER_AGENT'],"iPhone") || stripos($_SERVER['HTTP_USER_AGENT'],"iPad"))
	$device = 'ios';
Android
if (stripos($_SERVER['HTTP_USER_AGENT'],"Android") )
	$device = 'android';
LG Mobile
if (stripos($_SERVER['HTTP_USER_AGENT'],"webOS"))
	$device = 'webOs';