Limit search to post titles only

search post titlesI know that this is not something new on scene but anyway there snippet to limit search to the post titles only.

Yesterday client witch for I was working on a real estate template asked me: Is there a way to limit search to post titles only?

Here are the snippet that I’ve used to enable search for post titles only.

This search filter could be use various combinations to query any other custom post type. For example we could query custom post type “Portfolio” simply by replacing snippet with this code: $query->set(‘post_type’, ‘portfolio’);

Filter search to posts only

[php]
/**
* Snippet Name: Limit search to posts
* Snippet URL: http://wperror.com/limit-search-to-post-titles-only.html
*/
function SearchFilter($query) {
if ($query->is_search) {
$query->set(‘post_type’, ‘post’);
}
return $query;
}
add_filter(‘pre_get_posts’,’SearchFilter’);
[/php]

Filter search to titles only

[php]
/**
* Snippet Name: Limit search to titles only
* Snippet URL: http://wperror.com/limit-search-to-post-titles-only.html
*/
function __search_by_title_only( $search, &$wp_query )
{
global $wpdb;

if ( empty( $search ) )
return $search; // skip processing – no search term in query

$q = $wp_query->query_vars;
$n = ! empty( $q[‘exact’] ) ? ” : ‘%';

$search =
$searchand = ”;

foreach ( (array) $q[‘search_terms’] as $term ) {
$term = esc_sql( like_escape( $term ) );
$search .= "{$searchand}($wpdb->posts.post_title LIKE ‘{$n}{$term}{$n}’)";
$searchand = ‘ AND ‘;
}

if ( ! empty( $search ) ) {
$search = " AND ({$search}) ";
if ( ! is_user_logged_in() )
$search .= " AND ($wpdb->posts.post_password = ”) ";
}

return $search;
}
add_filter( ‘posts_search’, ‘__search_by_title_only’, 500, 2 );
[/php]

As you can see, the first snippet is used to limit search function to the posts only, while the other search by title only.

Customize WordPress Tag Cloud Widget

Yesterday I was working on developing custom template for one Czech guy. One of the things from the list is to do was customizing the WordPress Tag Cloud Widget for Categories. During this project there was a lot specific request, unique coding and tricks witch I’m gonna explain you here at WPError in next several days.

I was supposed to setup the Tag Cloud Widget to show only 11 categories by count from the categories list. Okay it might looks strange as we are talking about Tag cloud widget and categories in cloud, but let me explain it for you a little. The tag cloud is old WordPress function, but since WordPress 2.8 update it’s upgraded and it’s working with all taxonomies witch are by default tags and categories plus any other custom taxonomies.

[php]
// Parameters accepted by tag cloud widget
// @url http://codex.wordpress.org/Function_Reference/wp_tag_cloud
$args = array(
‘smallest’ => 8,
‘largest’ => 22,
‘unit’ => ‘pt’,
‘number’ => 45,
‘format’ => ‘flat’,
‘separator’ => \\"\n\\",
‘orderby’ => ‘name’,
‘order’ => ‘ASC’,
‘exclude’ => null,
‘include’ => null,
‘topic_count_text_callback’ => default_topic_count_text,
‘link’ => ‘view’,
‘taxonomy’ => ‘post_tag’,
‘echo’ => true,
‘child_of’ => null
);
[/php]

Limit Tag Cloud Widget to 11 items and Order by Count for Categories

Tag Cloud Widget

To achieve the following widget look I was suposed to edit few tag cloud parameters:
1. Customize Font Size of items (smallest and largest)
2. Customize the order
3. Custom taxonomy select
 
 
 
 
 
 

#1 working tag cloud widget code with array

[php]
add_filter(‘widget_tag_cloud_args’,’set_number_tags’);
function set_number_tags($args) {
$args = array(‘smallest’ => 11, ‘largest’ => 11, ‘number’ => 11, ‘orderby’ => ‘count’, ‘taxonomy’ => ‘category’);
return $args;
}
[/php]

#2 working tag cloud widget code without array

[php]
add_filter( ‘widget_tag_cloud_args’, ‘wpe_widget_tag_cloud_args’ );
function wpe_widget_tag_cloud_args( $args ) {
$args[‘smallest’] = 11;
$args[‘largest’] = 11;
$args[‘number’] = 11;
$args[‘orderby’] = ‘count';
$args[‘taxonomy’] = ‘category';
return $args;
}
[/php]

Here bellow I will write and explain you few other WP Tag Cloud snippets witch you can simply use simply by past into function.php file.

Changing the Number of Tags to Display on Widget

[php]
add_filter( ‘widget_tag_cloud_args’, ‘wpe_widget_tag_cloud_args’ );
function wpe_widget_tag_cloud_args( $args ) {
$args[‘number’] = 20;
return $args;
}
[/php]

Changing the Largest and Smallest Font Sizes

Keys that we are going to use are smallest, largest and unit. As we want to display pixels on font size instead of points we have to use unit key aswell.
[php]
add_filter( ‘widget_tag_cloud_args’, ‘wpe_widget_tag_cloud_args’ );
function wpe_widget_tag_cloud_args( $args ) {
$args[‘largest’] = 8;
$args[‘smallest’] = 12;
$args[‘unit’] = ‘px';
return $args;
}
[/php]

Excluding A Tag/Term From The Tag Cloud

Exclude key is comma separated term_id list to exclude. For example ‘exclude’ => 25,36 or $args[‘exclude’] = array(25, 36);. This means that tags with the term_id 25 or 36 will NOT be displayed on the widget.
[/php]
add_filter( ‘widget_tag_cloud_args’, ‘wpe_widget_tag_cloud_args’ );
function wpe_widget_tag_cloud_args( $args ) {
$args[‘exclude’] = array(25, 36);
return $args;
}
[php]

<h3>Including A Tag/Term From The Tag Cloud</h3>
Include key is exact opposite to the exclude key, same comma separated term_id list but this time to include in widget function results. For example ‘include’ => 18, 38, 40 or $args[‘include’] = array(18, 38, 40);. This means that tags with the term_id 18 or 38 or 40 will be the only displayed terms on the widget.
[php]
add_filter( ‘widget_tag_cloud_args’, ‘wpe_widget_tag_cloud_args’ );
function wpe_widget_tag_cloud_args( $args ) {
$args[‘include’] = array(25, 36);
return $args;
}
[/php]

Display only one category on home page

display only one category on home pageWell nowadays I found that there are many people looking for a plugin, snippet or hack to control displaying posts on the home page. This is snippet focused on displaying posts on the homepage from chosen category or categories.

So here is the snippet code or hack, now you just have to paste the code below in your functions.php file. Please be sure that you paste the code under the php tags.
 

 

 

PHP – Display only one category on home page

[php]
function my_category_queries( $query ) {
// only homepage and is the main query
if ($query->is_home() && $query->is_main_query()){
// display only posts in category with slug ‘articles’
$query->set(‘category_name’, ‘articles’);
// avoid sticky posts
$query->set(‘post__not_in’, get_option( ‘sticky_posts’ ) );
}
}<br />
add_action( ‘pre_get_posts’, ‘my_category_queries’ );

[/php]

Conclusion:
As you can see at the beginning of the function we are using the conditional functions is_home() witch is used for The Main Page and is_main_query() Is Main Query witch affect only to the main loop, and the function will proceed if both of conditions are true.
[php]$query->is_home() && $query->is_main_query()[/php]

The first $query-> ensures that all the posts are displayed with the category slug articles, this includes all Sticky Posts with any category, too.
[php]$query->set(‘category_name’, ‘articles’)[/php]

But then with the second $query-> set it to get all the posts with the category slug articles except for the posts that are sticky posts.
[php]$query->set(‘post__not_in’, get_option( ‘sticky_posts’ ) )[/php]

Additional notes:

Category Parameters
[php]
cat (int) – use category id.
category_name (string) – use category slug (NOT name).
category__and (array) – use category id.
category__in (array) – use category id.
category__not_in (array) – use category id.
[/php]

Let’s explain those parameters on our example now, well the first two commands works pretty same but the difference is that on first one we use Category ID’s and second one slugs. So basically if we wish to query the posts from two categories on the homepage we can simply update the code to ‘articles,affiliate-programs’ and function will call posts from the both categories on the homepage loop.

PHP – Display multiple categories on homepage

[php]
function my_category_queries( $query ) {
// only homepage and is the main query
if ($query->is_home() && $query->is_main_query()){
// display only posts in category with slug ‘articles’
// and slug ‘affiliate-programs’
$query->set(‘category_name’, ‘articles,affiliate-programs’);
// avoid sticky posts
$query->set(‘post__not_in’, get_option( ‘sticky_posts’ ) );
}
}

add_action( ‘pre_get_posts’, ‘my_category_queries’ );
[/php]

The example will be with the cat command instead of category_name will look like example bellow where the 25 and 36 are the category id’s.
[php]$query->set(‘cat’, ‘25,36’);[/php]

Different Background on each WordPress Post or Page

You may want to have a different background on your WordPress website. For example, you may have a celebrity blog and want to include an image of that celebrity on each of there pages or you may have a site that covers different topics and want to have a different color to represent each category.

This is easy to do with the plugin called Background Per Page.

Simply (in your WordPress dashboard) go to Plugins > Add New and search for Background Per Page, or download Background Per Page plugin, extract and upload the folder to the /wp-content/plugins/ directory. Then just Activate the plugin trough ‘Plugins’ menu in WordPress.

Download Background Per Page

How to Add Google AdSense into WordPress Part 2

In Part 1 I showed you how to add Google AdSense into your WordPress widgets. In part 2 I will show you how to add Google AdSense directly into your theme templates.

This means once its added it will continually add you code directly into each new page or post. In Part 3 i will show you which AdSense plugins are the best to use.

List of UK Towns, Cities & Counties for WordPress Categories & Database

Lots of people are forever searching the web for a list of UK towns, cities and counties.

Once they find it they are then stuck on how to use this info in there WordPress installation.

I have put together a video guide along with a CSV file for you to download all in the correct format for you to use straight away. it will take you know more than 5 minutes to use.

First off you can download a copy of the UK Towns, Cities and Counties Database Free Here.

You’ll need this free plugin, Go to Plugins > Add New and search for this in your WordPress dashboard WPEssence Bulk Categories

 

How to Add Google AdSense into WordPress Themes Part 1

Over a few tutorials i will show you how to add Google AdSense directly into your theme.
In Part 1 i will show you a really simple way to add your Adsense into a widgeted area. Then in part 2 i will show you how to add it directly into a theme file e.g. so that it adds AdSense directly into every post you make.

This is the YouTube video i have created showing you how to do this, If you find it useful please like it, add a positive comment for me and share it on your social network. All your support is appreciated.

Blogger vs. WordPress: Which is Better?

blogger vs wordpress
Foto: http://www.flickr.com/photos/d3so/

Doesn’t matter if you are journalist, small business owner or just another one who is searching various online money making ways, you will find yourself in a dilemma witch platform you should chose.

We all know that blogging and bloggers improved whole SEO system and that traditional websites witch usually have 7-8 pages. Nowadays, with your traditional website you can easy create a blog, where every single post you publish has its own URL. Suddenly you can go from 7-8 pages to 50, 100, or 1000+ it’s just matter of time.

Of-course that having more content pages is making easier for people to find you. Nowadays is known that search engines likes fresh content so don’t forget to regular update your site, that should help you rank better at search engine result pages.

Next and interesting thing about having a blog is building community and connecting with website visitors through comments and social sharing. This should be your main goal, this is what I will focus on this site too, converting readers into customers.

Now question is only why are u blogging? Is it for income, you wish to earn money, would u like to build authority site, business site what is reason for blogging? All those questions are very important because being successful blogger is not just writing posts, it is a way much more then that. You have to learn and understand a lot more stuff like search engine optimization, traffic generation, social media, research & reading and much more.

Once you are clear about the blogging reasons you should chose one of the free blogger sites like Blogger, Hubpages or WordPress.com or paid self hosting WordPress option.

The best, most popular and most important free blogging platforms are Blogger and WordPress – WordPress actually comes with both a free and a paid version.

What are advantages of using Blogger

Of-course the biggest advantage of using Blogger.com is that it is free, quick and easy to use. If you are cash-strapped, simply as that you can’t invest 10$ for domain and 7-8$ per month for hosting Blogger.com may be the perfect solution. It allows you to get some experience under your belt before investing time and money into self-hosting your blog.

We should never forget that Blogger is owned by Google there are many in-built features and services that can be useful for a new blogger, for example bloggers sites are hosted at Google servers so they have robust hosting, way much better security, faster indexing and maybe most important native support for AdSense.

The Advantages Of Using WordPress

WordPress is by my opinion absolutely best blogging software around. There are two versions of WordPress, free version at WordPress.com and ‘paid’ self hosted WordPress.org version.

WordPress.com is automatic and fully free version even they have paid model too. Once you register trough fully free process you will receive domain name yourdomain.wordpress.com while for an extra 15$ you can host your own domain name yourdoman.com /net /org … at WordPress servers. Unlike Blogger, you can control the look and feel of your new WordPress.com blog to a certain extent by selecting from one of their free WordPress themes. Themes control the colors, layout and fonts on your site so this is useful if you want your blog to look different to others.

The Downsides to both

While blogger laicks with many important plugns it allows adsense and other money making affiliate monterization. Still biggest problem is that none of your content is yours. You don’t actually own it belongs to Google and one wrong step can result in it being removed. Besides that WordPress.com is very strict when it comes to monetization. Most of revenue strategies are not allowed. Another WordPress.com restriction is that they don’t allow you to extend their default functionality of blog so basically, you are stuck with what they give you unless you pay for fancy extras.

Self-hosting WordPress Blog

WordPress stuff
Foto: http://www.flickr.com/photos/kchrist/

In my opinion Self-hosting WordPress blog is best option for every blogger, business owner or anyone who wish to success in a blogging on a long term. There are many advantages of using self-hosted WordPress. You can use various free or premium themes and plugins. This way you can extend your blog into serious well looking and high quality website. You can easy build quality community, collect followers, customers and sell stuff around. Your hands are free !!!

Which blogging platform do you use and why? Is this article being helpful for you, what is your choice Blogger or WordPress? Do you have a self-hosted blog? Please let me know in the comments below.

Top WordPress Plugins (free)

When you install your WordPress website you will want to add some WordPress plugins.

These are the recommended plugins you should add to every site to get you started…

Contact Form 7

By far the most popular contact form plugin. Simple to use and an absolute must if you want people to be able to contact you. See an example here.

Yoast SEO

Want to be found by the search engines? Not an SEO expert? Don’t worry, this WordPress SEO plugin (from one of the most respected SEO’s in the world) is free and easy to install and makes your onpage SEO really easy with a simple check list. It also autogenerates you an XML sitemap (for posts, pages, categories and images) that you can add to Google Webmaster Tools.

seo

Related Posts via Categories

Blogs generally have a high bounce rate (a user visits one page then leaves the website). This is because they have found the information they want then leave your website. This plugin suggests other posts they may like and keeps them on on your website.

ShareThis

With everyone using social media these days (Facebook, Twitter etc) you need to make it easy for your users to share your content and this plugin does exactly that.

YouTube Sidebar Widget

If you have created YouTube videos for your blog then you need a YouTube plugin to add your video thumbnails to your sidebar.

All in One Adsense and YPN Pro

Have a Google AdSense account? This plugin makes it quick and easy for you to set up your website monetization. (Adsense account required)

Google Analytics for WordPress

Another awesome plugin from Joost de Valk (WordPress SEO Plugin creator). Track your sites visitors activity with this easy to use plugin. Google Analytics account required.

That should be enough to get you started, and don’t forget to change your Settings > Permalinks to post tag so you get SEO friendly URLs 😉