Adding Categories To A WordPress Post

I’ve been writing a custom WordPress plugin to accept emailed posts and insert them into a WordPress blog. One of the difficult issues I’ve encountered is that WordPress deals with tags and categories in different ways: it’s relatively easy to add and remove tags. It takes a bit of roundabout work to do the same with categories.

The below code fragment takes a string array of categories ( $to_post_categories ) and sets them onto a post (the ID of the post is in $created_post_id ).

	//Set post categories
	/**
	 * WP doesn't allow us to set categories as easily as tags. With tags we can simply declare 
	 * the names of the tags and be done with it. WP requires that we pass in the ID of the category, 
	 * and if the category doesn't exist, we need to create it. So we loop through the array of 
	 * category names, see if they exist (and if so, collect the ID) and if they don't exist, 
	 * create the category and map the ID to the post.
	 **/
	 foreach ($to_post_categories as $category_name) {
		 $category_id = get_category_by_slug($category_name);
		 if ($category_id == false) {
			 //Category doesn't exist, create it
			 $category_id = wp_create_category($category_name);
		 }
		 
		 //category_id now holds ID of right category or recently created category.
		 //Add category to post
		 wp_set_post_categories($created_post_id, array($category_id), true);
	 }//end looping through post categories sent to us

Newsworthy And Non-Newsworthy Searches

I stumbled upon this interesting article from The Verge, where YouTube modified its search results by tagging Brie Larson as part of the news: https://www.theverge.com/2019/3/8/18255265/brie-larson-youtube-captain-marvel-mcu-algorithm-review-bomb-trolls .

In short, YouTube searches for Brie Larson were initially returning videos about boycotting the movie Captain Marvel. By tagging Larson as a news item, the search results immediately changed to reflect videos from authoritative news services: ABC, CBS, Entertainment Tonight, and so forth. This is a useful function for most people searching, as most users will be looking for late night interviews, news media reports, and so forth.

A search for Brie Larson on YouTube returns videos from news services – note the Top news notice on the top of the image.

As this article demonstrates, search context can be very important. To fully learn about a topic, it’s vitally important to search Google, review the results, then make more searches that are informed by your previous searches. Let’s say you’re a journalist, and want to write about Brie Larson. You’d start out with a general Google and YouTube search about Larson. Then by reviewing the search results (at least the first 2-3 pages of results) you’d learn that there was controversy over Larson playing Captain Marvel. Then you could search for Brie Larson Captain Marvel. Then Brie Larson controversy.

Possibly you might dig a bit deeper and search for Brie Larson boycott. After you’ve exhausted that route, follow other discussion threads: for example, searching for Brie Larson fans, or Captain Marvel box office numbers.

A search for Brie Larson boycott reveals further information for an aspiring journalist. Why is there a boycott? Further Google searching would help.

There are numerous ways that a good journalist could dig up even more information about this issue – for example, why not use Google’s date searching feature to exclude recent news reports and only search earlier postings?

Googling current-news topics can be difficult, as you’ll see many current news items pop up on your results. With intelligent Googling, you can extract useful knowledge about almost anything.

Distance Search: The AROUND(#) Search Operator – The Most Underrated Google Search Function

The AROUND(#) search operator is one of my favorite, and frankly underrated, search functions. It is a distance operator between two words; in other words, it searches for web pages that have two words together, with no more than # of words separating them.

Here’s a simple example. Suppose I tried searching for bacon AROUND(3) cheeseburger.

bacon AROUND(3) cheeseburger

Google returns the expected bacon cheeseburger results, but there are also some interesting, unexpected results. The second result is for a bacon avocado cheeseburger (1 word – avocado – separating our search terms of bacon and cheeseburger). The third result is for a bacon ultimate cheeseburger (again, 1 word separating bacon and cheeseburger). The fourth result is for cheeseburgers with bourbon bacon (2 words – with bourbon – separating our search terms).

Use the operator whenever you need to find two words closely associated with each other, but possibly modified by other words. Try not to use a high # with AROUND(#) – I would suggest no more than 5 unless there’s a really good reason for a greater distance.

Google Doodle: International Women’s Day

Today, March 8 2019, Google celebrates International Women’s Day with a doodle. Here’s how the doodle looks like on the Google home page:

Clicking on the doodle makes it expand:

And then moves into a slideshow with quotes from famous women:

Clicking on the search option goes to a Google search for International Women’s Day. Clicking the share option goes to a screen to share on Twitter, Facebook and other social media:

Searching The Content Of A Web Page: The intext: and allintext: Search Operators

Among the less useful of operators are the intext: and allintext: search operators. As the title says, these operators require that the given word(s) show up in the content of a web page. For example, if you searched for intext:stock (no space between intext: and the searched keyword), the returned web pages would have the word stock as part of the web page:

intext:stock

Similarly, if you searched for allintext:stock dis, you would get web pages with the words stock and dis within their text content:

allintext:stock dis

While these operators are important to remember, they’re not as useful as their intitle/allintitle/inurl/allinurl counterparts. In the vast majority of cases, skipping the intext: search function and searching on the same key words would result in the same, or largely the same, search results as using the operators.

Searching URLs: The inurl: and allinurl: operators

An URL is an Uniform Resource Locator – it is the https://… gobbledegook on the top of your web browser. It’s also frequently called the web address, or just address. I will be using the words URL and address interchangeably, and you can as well.

The inurl: and allinurl: search operators search for specific words in web page URLs. These operators work best when you’re searching for product pages, or blog entries.

A good example would be to look at an Amazon product page; here’s the URL to order an Amazon gift card:

https://www.amazon.com/Amazon-Amazon-com-eGift-Cards/dp/B004LLIKVU

As you can see, Amazon describes the product – an Amazon gift card – in the URL itself: Amazon-Amazon-com-eGift-Cards. You’ll see this design frequently in online stores, blogs, and so forth: it helps optimize the site for search engines such as Google.

Suppose you wanted to search for gifts on Google. You might start out by searching for the following:

inurl:gift

That works – as you can see, all the URLs (green text) have the word gift in them. But the links aren’t useful: I wanted gift card information, not just general information about the word gift.

In this case, I can turn to the allinurl: operator, which will require that all the words I list should be in the URL. Let’s try:

allinurl:gift card amazon

Now that search was useful – all of these URLs (green text) contain the words amazon, gift and card.

In general, while the inurl: and allinurl: operators are incredibly useful, I would recommend trying the intitle and allintitle: operators first. Web page titles tend to be more detailed and have more space than URL addresses. inurl and allinurl are more useful for the times where you’ve forgotten a certain web page URL – the address is just on the tip of your tongue, but you can remember fragments of it – inurl and allinurl can help you reconstruct it.

Searching Web Page Titles: intitle: and allintitle:

Each web page has a title – depending on your web browser, the title of a web page is shown as part of the tab or in the browser’s title bar. For example, in the picture below, the title of CNN.com is CNN – Breaking News, Latest News and Videos.

To search web pages with specific words in their titles, use the intitle: and allintitle: operator. For example, to search all web pages containing the word rome in their titles, you can search for:

intitle:rome

Suppose I was only interested in traveling to Rome – that entry to Wikipedia doesn’t help me figure out how to travel there. I might try the allintitle: operator, which searches for web pages containing all the words given in the title. For example:

allintitle:rome travel

The allintitle: operator works best with a few key words – remember, you’re searching web page titles which are usually short and to the point.

Finding Related Websites – The Related: Operator

As Google indexes the Internet, it can make connections between related websites and content. Take advantage of these connections by using the related: operator.

The related: operator shows related web sites. For example, if I search for related:chase.com, I’ll get a list of banks:

related:chase.com

This tool is useful when you’re trying to find competitor services. For example, if I was looking for a job, I would be looking for job sites to search postings and add my resume. I know that indeed.com is one job board. I can find other job sites by using the related: operator:

related:indeed.com

Google Doodle: Desi Arnaz

Today’s Google Doodle celebrates Desi Arnaz, best known as playing Ricky Ricardo in the TV show I Love Lucy. Here’s how the Google page looked like with the doodle:

The doodle itself:

Clicking on the doodle links you to a search for Desi Arnaz:

Clicking on the link to explore the life of Desi Arnaz brings you to a Google Arts & Culture article: