Categories
Programming

Finding, Deleting and Cleaning Orphaned Post Meta in WordPress

NOTE: Please remember to backup your database before doing any SQL level deletes.

Over time, the wp_postmeta table can get littered with a number of records. These linked meta data rows are not always removed when posts from the wp_posts table are deleted. While WordPress has well optimised the wp_postmeta table, it might be wise to clean it from time to time. I had over 250,000 orphan records after 4 years of running a business website – mostly due to events and ACF plugins!

[sql]

— List all orphan rows from wp_postmeta
SELECT * FROM wp_postmeta
LEFT JOIN wp_posts ON wp_posts.ID = wp_postmeta.post_id
WHERE wp_posts.ID IS NULL;

— Delete all orphan rows from wp_postmeta
DELETE wp_postmeta FROM wp_postmeta
LEFT JOIN wp_posts ON wp_posts.ID = wp_postmeta.post_id
WHERE wp_posts.ID IS NULL;

[/sql]

I hope the above helps.

Categories
Programming

Which mode of MySQL WEEK() complies with ISO 8601 and matches PHP’s DATE function

To mix MySQL week numbers with PHP, can be a bit confusing. The best way to get a relevant match is to use mode 3 in the MySQL WEEK() function.

For example, in PHP:
[php]
date(‘W’, strtotime(‘2019-12-31’));
Returns: 01

date(‘W’, strtotime(‘2020-12-31’));
Returns: 53

date(‘W’, strtotime(‘2021-12-31’));
Returns: 52
[/php]

The same in MySQL:
[sql]
SELECT WEEK(‘2019-12-31’, 3);
Returns: 1

SELECT WEEK(‘2020-12-31’, 3);
Returns: 53

SELECT WEEK(‘2021-12-31’, 3);
Returns: 52
[/sql]

When mixing up the data in PHP, you might have to at a leading zero to weeks 1 to 9 to match the MySQL week numbers with PHP’s week numbers.

Source: StackOverflow

Categories
Programming

SugarCRM / SuiteCRM – Adding created_by and modified_user_id using SugarBean

If you are a big fan of using SugarBean to handle CRUD (Create Read Update Delete) operations, you might have noticed that the created_by and modified_user_id values are automatically provided by the underlying code logic. You can bypass this logic and override the UserIDs. Below is the example PHP code:

[php]
$bean->set_created_by = false;
$bean->update_modified_by = false;
$bean->created_by = "uuid-of-the-user";
$bean->modified_user_id = "uuid-of-the-user";
$bean->assigned_user_id = "uuid-of-the-user";
[/php]

Also, SugarBean automatically provides the date_entered and date_modified values. Use the following if you’d like to override them.

[php]
$bean->date_entered = "Y-m-d H:i:s";
$bean->date_modified = "Y-m-d H:i:s";
$bean->update_date_entered = false;
$bean->update_date_modified = false;
[/php]

Ref: https://github.com/salesagility/SuiteCRM/blob/master/data/SugarBean.php

Categories
Programming

Magento – Inspect the Data Passed Through Observer Events

[php]
echo "<pre>";
var_dump($observer->getDataObject()->getData());
[/php]

Categories
Programming

Magento – Show Address Fields in Customer Registration Form

Open up your local.xml or any xml file that you are using with your theme. Add the following bit:

[xml]
<customer_account_create>
<reference name="customer_form_register">
<action method="setShowAddressFields">
<param>true</param>
</action>
</reference>
</customer_account_create>
[/xml]

You will notice a code in the register.phtml file found here: /app/design/frontend/base/default/template/customer/form/register.phtml

[php]
if($this->getShowAddressFields()):
[/php]

This is the condition that shows the customer address related fields in the customer account creation page.

Also, you can copy into your theme and edit the register.phtml file to modify the layout of the address form.

Categories
Programming

WordPress Fix – the_date() Function Returning Blank Value

Are you are using the_date() function in WordPress templates and getting a blank or empty value?

When more than one post is published on the same day, the_date() function displays only the date for the first post in the loop.

The right way to handle this is by using the the_time() function.

Try this:
[php]
<?php the_time(‘F j, Y’); ?> at <?php the_time(‘g:i a’); ?>
[/php]

Categories
Programming

Vagrant – Removing VM from Global Status after Deleting Folder and Files

You might have deleted the folders and files for a Vagrant virtual machine. However, you will see the machine appear under the ‘vagrant global-status’ command.

If you issue the command ‘vagrant destroy [virtualmachineid]’, you will get the following message

“The working directory for Vagrant doesn’t exist! This is the specified working directory:”

To remove the machine completely, issue the following command. ‘vagrant global-status –prune’. This will fix it.

Categories
Programming

Magento – Get Current Package Name and Theme Name

[php]

// To get the current package name of the Magento store / site

Mage::getSingleton(‘core/design_package’)->getPackageName();

// To get the current theme of the Magento store / site

Mage::getSingleton(‘core/design_package’)->getTheme(‘frontend’);

// You can pass ‘locale’, ‘layout’, ‘template’, ‘default’, or ‘skin’ into the above function.

[/php]

Categories
Programming

Magento – Disable Toolbar Sort / Order By Memory Cookie

[xml]
<reference name="product_list_toolbar">
<action method="disableParamsMemorizing"/>
</reference>
[/xml]

Categories
Programming

WordPress – Changing The Site URL Using WP-CLI Command Line Tool

The WP-CLI command line tool is a handy application to run various WordPress tasks.

Download the WP-CLI command line tool into the WordPress root folder using CURL or WGET.

[bash]
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
[/bash]

Check if the tool is working.

[bash]
php wp-cli.phar –info
[/bash]

Not run the search and replace command to look through all the core tables to change the URL.

[bash]
php wp-cli.phar search-replace ‘http://www.olddomain.com’ ‘http://www.newdomain.com’
[/bash]

This will save you numerous hours and give you some peace of mind!