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

Explanation:

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.id = wp_posts.post.id = 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');

How to find all symbolic links junctions on windows drive

TL;DR::cmd (administrator) dir /AL /S C:\ 

Or dir /AL /S C:\ > junctions.txt
To print the output to a file. which is better.

Does Windows have symbolic links like Linux?

Yes. windows has symbolic links also called junctions.  What are junctions? they are simply links to another location on a hard drive or network drive. The operating system and most programs will treat symbolic links like if they were actual folder. This is very useful when dealing with low disk space especially on pricey ssd (although these are getting cheaper) or eMMC on laptops.   Some programs simply insist on storing their data on C application data. but they aren’t essential apps that need to run all the time. Things like Android studio or Netbeans.  you may run one or the other most of the time. but still these programs store large amount of data.

Break down of the command:

DIR:  list files and folders in current working directory or specified one.
AL:   A switch is to display files with specific attributes such as hidden files, directories, read only.   L attribute is for Junctions (Reparse Points)
S:  means to include System files in the results.
>  redirects output to a file.

How to read an image from url in python 3 and get the height and width

To read an image directly from url and then reads its size. you can use urllib.request and PIL aka Pillow.
If you don’t have pillow installed. you can use:

  1.  python -m pip install Pillow

and before that you might need to upgrade pip, especially if you get an error

  1.  python -m pip install --upgrade pip

Here is how to read an image from url.

  1.  import urllib.request
  2.  from PIL import Image
  3.  url = 'http://www.example.com/my_image_is_not_your_image.png'
  4.  image = Image.open(urllib.request.urlopen(url))
  5.  width, height = image.size
  6.  print (width,height)
  7.  

If you followed a tutorial that uses urllib2 you can change that to urllib.request in python 3.

‘cp932’ codec can’t encode character ‘\xa1’ in position 62: illegal multibyte sequence

When using print in python with some multibyte character or when decoding from another encoding you might encounter this fatal error:

  1.  Traceback (most recent call last):
  2.   File "skynet.py", line 28, in
  3.   print(skyname_wakeup_word)
  4.  UnicodeEncodeError: 'cp932' codec can't encode character '\xa1' in position 62: illegal multibyte sequence

To sort of fix it, you can use

  1.  skyname_wakeup_word.encode('cp932',"ignore")

This occurs because python can’t convert that character to the traditional windows ansi used in windows terminal (cmd). cp932 is code page 932 the Japanese version of shift-JIS.
Other encodings: cp874, cp936, cp949, cp950, cp1251, cp1252, cp1253, cp1254, cp1255, cp1256, cp1257, cp1258. Source