WordPress & WooCommerce
EXPERT
Do you need to create or customize your WordPress site or WooCommerce store?
I help you with that
Code

Find Posts with the Most Number of Revisions

SQL - find post revisions

I was working on a content-heavy website that have a very big wp_posts table. This obviously caused by a revisions. To confirm this I needed to find out which where the posts with the most number of revisions. As I had guessed, I found out revisions where killing the wp_posts table. Quite a few posts had as many as 100+ revisions.

To find this out I came up with this SQL query then I ran in phpMyAdmin:

SELECT parent.ID, parent.post_title, count(children.post_parent) AS children_count 
  FROM wp_posts as parent
LEFT OUTER
  JOIN wp_posts as children
    ON children.post_parent = parent.ID && children.post_type = 'revision'
  WHERE parent.post_parent = 0
GROUP
    BY parent.ID
ORDER BY children_count DESC
LIMIT 50

Of course, the first thing I did after I found this out was limiting the revisions in WP by putting this line in wp-config.php

define('WP_POST_REVISIONS', 2);

I highly recommend doing this for content-heavy WordPress sites.

Read More
Code

Assign Authors or Co-authors to a WordPress Posts Programmatically

Assign author(s) to a WordPress post programmatically

There are occasions when you need to do something programmatically, i.e., through a little function, rather then by clicking on a button in WordPress. Such was the case when I needed to re-assign an author to massive amount of posts. There were just too many posts to do it manually from the backend of WordPress, so I had to accomplish this through a little piece of code.

Read More
Code

Overwrite Default Post Permalinks

wordpress custom field example

Every WordPress post has a permalink by default. The permalink is displayed, as expected, by the the_permalink() function. In some cases, we might need to enforce another URL to a post as its permalink, be it another link within the site or an external URL. We can provide the custom URL through a custom field that we will conveniently name overwrite_permalink.

Read More
Code

A Guide to Actions and Filters in WordPress

wordpress hooks: actions and filters

WordPress comes with this built-in API that allows us to hook custom functionality into the rest of WordPress. We can provide that extra functionality through plugins or themes. These are called hooks and they are either actions or filtersActions allow us to run custom functions at a certain points of a request, while filters allow us to process and change data at these certain points.

The gist of the difference between actions and filters is:
Actions modify functionality; filters modify data.

Read More
Code

How to Get the Post Slug

The post slug is the part of the URL of a post or page at the last section. E.g.:

Page: http://wplancer.com/about/
Post: http://wplancer.com/2015/09/post-slug-here/

There are two ways to get the slug in WordPress inside the loop. The first way is to access the $post object that is available inside the loop.

$slug =  $post->post_name;

The second way is to pass the whole permalink to the basename function, which will automatically process the URL and give us only the slug.

$slug = basename(get_permalink());

Happy coding!

Read More
1 2 3 15