How to join tables to wordpress posts properly not in a hacky way?

In sum;

In function.php of your theme:

function my_theme_join_mytable_to_WPQuery($join) {
    global $wpdb;
    $join .= " LEFT JOIN {$wpdb->prefix}mytable ON $wpdb->posts.ID = {$wpdb->prefix}mytable.postid ";
    return $join;
add_filter('posts_join', 'my_theme_join_mytable_to_WPQuery');
Join tables

Jigsaw puzzle symbolizing joined tables © Ajv123ajv | Dreamstime Stock Photos


If you want to join tables in wordpress whether left join or inner to wordpress posts query. Say if you need to display some custom rating, game scores or any other data you want to associate with the table.  It’s more efficient to join the table to the posts database table using wordpress function hooks.  Rather than fetching your custom tables inside the template on the fly.

The benefit of using wordpress hooks and functions is not just efficiency. in using database queries. but also you can let other plugins you install like database caches or other processes to work with your custom tables without any extra steps.

You can use functions.php in your theme or your custom plugin to hook to the posts_join wordpress api.  you need to register table join hook filter with the following command:

add_filter(‘posts_join’, ‘my_theme_join_mytable_to_WPQuery’);

What this does, it tells wordpress to call your custom function my_theme_join_mytable_to_WPQuery  when building the posts fetching query. so every time a user views your site and sees the list of posts or search posts. the function will be called before querying the database.  The filter hook passes the join statement sent by user.

Then add a function with the custom name you’ve chosen earlier: my_theme_join_mytable_to_WPQuery

function my_theme_join_mytable_to_WPQuery($join) {


The join variable is a string that contains the join statements added by other filters or wordpress.  you need concatenate to it. as:

$join .= " LEFT JOIN MYTABLE ON = = MYTABLE.postid";

then you must return join back

return $join;

It’s better however to use wordpress table names and prefixes.  it’s possible the prefix of tables not wp_ or table names for wordpress change.  instead $wpdb->prefix to get the prefix  and $wpdb->posts to get wordpress post table (this includes the prefix).  I assume your custom table also has the same wordpress prefix as it should.

therefore your table becomes {$wpdb->prefix}mytable

You need to import the variable $wpdb from the global scope.  using global $wpdb.

Finally it would look like this:


function my_theme_join_mytable_to_WPQuery($join) {
    global $wpdb;
    $join .= " LEFT JOIN {$wpdb->prefix}mytable ON $wpdb->posts.ID = {$wpdb->prefix}mytable.postid ";
    return $join;
add_filter('posts_join', 'my_theme_join_mytable_to_WPQuery');

Magento: How to hide products that have no images

If you would like to hide products without images in your Magento store product list page, this is one way to do it. edit app/design/frontend/[YOUR CURRENT TEMPLATE]/[YOUR CURRENT THEME]/template/catalog/list.phtml (e.g. app/design/frontend/default/default/template/catalog/list.phtml)

Change :

  1.  $_productCollection=$this->getLoadedProductCollection();


  1.  $_productCollection = clone $this->getLoadedProductCollection();
  2.  $_productCollection->clear()
  3.   ->addAttributeToFilter('image', array('neq' => 'no_selection'))
  4.   ->load();

Since the collection we get from getLoadedProductCollection was already fetched from database, we cannot add attribute filters to it, that’s why it wouldn’t work without the clone. So we need to clone it then clear it. then add the attribute filter.

If you are not familiar with neq, neq means not equal.

Magento: How to calculate tax programmatically

This is how to calculate tax on a given price and product programmatically in Magento.

  1.  $priceInclTax=Mage::helper('tax')->getPrice($product,$price);

You can get $price from $product using

  1.  $price=$product->getFinalPrice();

You can get price the way you want, it can be custom price or any number you want to calculate tax on, but you need to pass the $product which should be a valid product object.