How to list page or category links in WordPress

These simple peices of code will allow you to display page or category links in your WordPress theme.

To list pages use the following code:

You can exclude specific pages by changing the above code to the following:

Just replace 1 with the ID of the page and add a coma to list more than one.

To list categories use the following code:

You can exclude specific categories by changing the above code to the following:

Just replace 1 with the ID of the category and add a coma to list more than one.

How to mass change article slug urls in WordPress

This piece of PHP code will mass change your WordPress slug links to default slugs WordPress would normally generate from your article titles.

Open wp-config.php, add the following at the bottom:

Save and upload your new wp-config.php and load your home page only one time. Once the loading is completed, remove the code from your wp-config.php file.

Credits: Clorith @ https://github.com/Clorith/wp-slug-update

How to add custom avatar in WordPress

Gravatar has become really easy to use custom avatars across multiple platforms. What if your users don’t use Gravatar or have an avatar, a default avatar is loaded in it’s space. It is recommended you brand your default avatar to your website.

First, lets make sure you have a custom avatar image ready. If you do not, make one in the dimensions of 64×64.

Second, name your avatar whatever you want, let’s say “custom-avatar.jpg”. It’s entirely up to you to name it what you want.

Thirdly, upload your image to wp-content/themes/your-theme/images/.

Lastly, open wp-content/themes/your-theme/functions.php and add the following:

// Adds a custom default avatar
if ( !function_exists(‘fb_addgravatar’) ) {
function fb_addgravatar( $avatar_defaults ) {
$myavatar = get_bloginfo(‘template_directory’) . ‘/images/custom-avatar.jpg’;
$avatar_defaults[$myavatar] = ‘Your Name’;
return $avatar_defaults;
}
add_filter( ‘avatar_defaults’, ‘fb_addgravatar’ );
}

Note: Replace “custom-avatar.jpg” with whatever you named it. Also replace “Your Name” with a name to describe the image – this is normally your website name.

Now go to Settings —> and click on “Discussion”, you will be able to see the custom avatar you just added. You can now choose the newly added custom avatar as the default avatar shown.

Helpful SQL queries to run in WordPress

Here are a list of great queries to run on your WordPress installation.

Remove all spam comments from the database

DELETE FROM HYv3g_comments WHERE HYv3g_comments.comment_approved = 'spam';

Globally enable comments for all users

UPDATE HYv3g_posts SET comment_status = 'open';

Globally disable comments for all users

UPDATE HYv3g_posts SET comment_status = 'closed';

Globally enable comments for registered users only

UPDATE HYv3g_posts SET comment_status = 'registered_only';

Globally enable/disable comments before a certain date

UPDATE HYv3g_posts SET comment_status = 'closed' WHERE post_date < '2008-01-01' AND post_status = 'publish';

Globally enable pingbacks/trackbacks for all users

UPDATE HYv3g_posts SET ping_status = 'open';

Globally disable pingbacks/trackbacks for all users

UPDATE HYv3g_posts SET ping_status = 'closed';

Globally enable/disable pingbacks/trackbacks before a certain date

UPDATE HYv3g_posts SET ping_status = 'closed' WHERE post_date < '2008-01-01' AND post_status = 'publish';

Globally enable/disable all discussion: comments, pingbacks and trackbacks

UPDATE HYv3g_posts SET comment_status = 'open', ping_status = 'open' WHERE comment_status = 'closed' AND post_status = 'publish';

Globally enable/disable comments, pingbacks and trackbacks before a certain date

UPDATE HYv3g_posts SET comment_status = 'closed', ping_status = 'closed' WHERE post_date < '2008-01-01' AND post_status = 'publish';

How to remove pages from search results in WordPress

Removing pages from the search results can be a plus, specially when your visitors are looking for the rich content and not a page.

Open wp-content/themes/your-theme/functions.php, add the following:

// Removes pages from search results
function SearchFilter($query) {
if ($query->is_search) {
$query->set('post_type', 'post');
}
return $query;
}

add_filter('pre_get_posts','SearchFilter');

Simply remove the above code if you wish to allow pages to be shown again in search results.