WordPress query posts my meta data


On a WP powered site, if you add custom meta information, you might want to query the posts that have a particular meta information, like a custom post template.


This is done very easy and safe using the WP_Query() function with following arguments:

$args = array(
‘post_type’ => ‘post’,
‘meta_query’ => array(
‘key’ => ‘custom_post_template’,
‘value’ => ‘templatename.php’
$queryPosts = new WP_Query( $args );

while ($queryPosts->have_posts()) {
//do something with each post

In the while() loop you can do anything with the post object.

Posted in Wordpress | Tagged , , | 1 Comment

nginx index inside conditional location


On a server that delivers multiple sites, I need to set up the location of a request depending on the host. For example I need to serve different text depending on the site language.


The only problem here is that inside the location declaration there cannot be a root or index defined inside an if clause. The way to do it is to set up a variable from the if clause and use it outside of the if:

location /subpage/ {
if ($http_host = “mysite.fr”) { set $country “fr”; }
if ($http_host = “mysite.es”) { set $country “es”; }
index /local/files/$country/index.html

Posted in Best practices, nginx, System | Tagged , | Leave a comment

Facebook Instant Articles feed not being crawled


After installing in WordPress one of the Facebook Instant Articles plugins, the feed can be seen in browser at https://www.example.com/feed/fb/ but the Facebook publishing tool can’t crawl it.


Of course this can be a due to a series of issues, but the ones I came across are:

  • URLs in the feed have to match the domain and be absolute, including the canonical links in the content
  • Content of https://www.example.com/feed/fb/ should match the one in the regular feed: https://www.example.com/feed/
  • The feed shouldn’t have internal redirects, which sometimes WordPress creates
Posted in Wordpress | Tagged , , , | Leave a comment

Check database tables size in Mb


How can I check the size of my database tables and receive a human readaable result? This can be useful when deciding a storage architecture or a Cloud storage plan.


From command line, go to mysql> terminal:

SELECT table_name AS “Table”, ROUND(((data_length + index_length) / 1024 / 1024), 2) AS “Size (MB)” FROM information_schema.TABLES WHERE table_schema = “database_name” ORDER BY (data_length + index_length) DESC;

Posted in SQL | Tagged , | Leave a comment

WordPress proper query_posts with real results


WordPress query_posts() function is pretty powerful, and easy to read out. However, you need to know what you’re querying for, as very often the results are not the expected ones. The specific post meta query is covered here.


Take care of pagination. For regular queries, you don’t need that impediment, and somehow WordPress does it on it’s own. How to avoid it? Add a huge number of posts_per_page to the arguments array:

$args[‘posts_per_page’ ] = 99999;

You need to exclude tags fast, based only on the slug? Nothing simpler:

$args[‘tag__not_in’] = array(get_term_by(‘slug’, TAG_1, ‘post_tag’)->term_id, get_term_by(‘slug’, TAG_2, ‘post_tag’)->term_id, get_term_by(‘slug’, TAG_3, ‘post_tag’)->term_id);

You can find out an id of a term very easy like this. Works as well for categories:

$categoryId = get_term_by(‘slug’, CATEGORY_SLUG, ‘category’)->term_id;

You can calculate the number of valid posts you are actually displaying by your rules by combining the arguments:

‘tag__not_in’ => array(get_term_by(‘slug’, TAG_1,      ‘post_tag’)->term_id, get_term_by(‘slug’, TAG_2, ‘post_tag’)->term_id),
‘post_type’ => ‘post’,
‘post_status’ => ‘publish’,
‘category_name’ => CATEGORY_SLUG,
‘posts_per_page’ => 99999,

$nrOfPostsInCategory = count(query_posts($args));

This actually returns the number of published posts in a category, without the ones that have the excluded tags. This comes in useful when you want to use functional tags, that aid your code or they way you use some posts.

Posted in Wordpress | Tagged , , , , | Leave a comment

Retrieve coordinates when click on image


HTML forms allow the use of an image as a submit button, and the action triggers the send of the x and y coordinates of the click relative to the image size. However, why not take it to the next level, and record on the go the image click coordinates?


1. Create a <div> containing the image, with an id and an action name for the onmousedown event:

<div id=”image_wrapper” onmousedown=”recordClick(event)”>

2. Create a wrapper for the results(div, table, whatever you want):

<div id=”coordinatesOutput”>Coordinates will be here</div>

3. Add the magic javascript:

var coordinatesArray = [];
var i= 0;

function recordClick(event) {
coordinates[i] = x_pointer+’,’+y_pointer;
document.getElementById(“image_wrapper”).style.cursor = ‘crosshair’;

function printCoordinates(){
var coordinates = “”;
for(var j=1;j<coordinatesArray .length;j++){
coordinates += coordinatcoordinatesArray[j]+'<br />;

document.getElementById(‘coordinatesOutput’).innerHTML = coordinates;

4. Just put it altogether with some styling in a nice HTML block, and enjoy!

Posted in Javascript, Script | Tagged , , , | Leave a comment

Change button text value on HTML input type file


Browsers render by default “Choose file” (or similar, depending on browsers and locale settings) when an input of type “file” is detected.


Unfortunately there is no easy solution.

 <input type=”file” name=”inputFile” id=”inputFile” />

This is pretty much all you can do with HTML.

The only solution, but a bit too much overhead, is to add javascript or Flash. Or you think that the default “Choose file” is beautiful and you live with it.

Posted in Best practices | Tagged , , | Leave a comment