• Skip to main content
  • Skip to primary sidebar

Victor Font Consulting Group, LLC

The DEX Intranet Specialists

  • Home
  • Care Plans
    • Care Articles
    • Optional Subscriptions
  • Consultations
  • Products
    • Code Snippets
    • Public GitHub Repositories
    • Gist Snippets
    • Pastebin Snippets (Free)
    • Free Plugins
  • FAQs
  • Support
    • Graphic Design
  • Contact
    • Speakers
    • Portfolio
  • Resources
    • Free WordPress Video Training
    • Tutorials
    • Articles
    • Cybersecurity
    • EU Referral Network

Add CPTs to WordPress Searches

September 21, 2015 By Victor M. Font Jr.

Add CPTs to WordPress Searches Radar screen, public domain image courtesy of pixabay.com

If you've used WordPress for any length of time, you are no doubt familiar with the concept of posts and pages. To technocrats, these are known as post types. As delivered, WordPress provides five (5) different post types that are either readily accessible to users or used internally by WordPress. These post types are post, page, attachment, revision, and navigation menu. While these post types suit the average blogger, as a web development platform, WordPress is capable of far exceeding the needs of the average user through the use of hooks, filters, plugins, and custom post types (CPTs).

For example, using WordPress to sell products through an eCommerce solution, such as the very popular WooCommerce, is just one example of how WordPress can be extended through plugins and custom post types. CPTs are new post types you can create for whatever need or purpose you see fit. The WooCommerce individual product pages are examples of a custom post type designed to display products. The product custom post type is created when the WooCommerce plugin is installed.

The Stealthiness of CPTs

Because CPTs are custom components, they can be quite stealthy when it comes to searching through their content. In fact, they are completely off the radar unless you intentionally do something about it. WordPress's search query only "knows" about its own 5 post types, and at that it only searches through post and page content. If you want to include your CPTs in the search query, you have to modify the query parameters to include them.

The following code is used on this site to include our portfolio custom post type in the site's search results page.

Note: This change will also change the way searches execute across the entire WordPress spectrum. For example, when you are searching for an internal link in the post or page editor. To confine the search to the front end only, change line 3 above to read:

How the Snippet Works

The developers behind WordPress are smart people…very smart people! As WordPress has evolved, they gave us the ability to extend WordPress's functionality through hooks and filters. Hooks and filters allow us to 'hook into' the rest of WordPress. In other words, they allow us to work with built-in WordPress functions whenever we need to do something that modifies their behavior. In this case, we are modifying the default behavior of the search query. We do this by hooking into the built-in WordPress pre_get_posts filter. A filter, by the way, is just a type of hook.

The WordPress codex explains the pre_get_posts filter this way:

This hook is called after the query variable object is created, but before the actual query is run.

The pre_get_posts action gives developers access to the $query object by reference (any changes you make to $query are made directly to the original object - no return value is necessary).

The $query object is pretty complex in itself. As a core WordPress feature, it is a work horse. The $query->set() function allows us to set a named query variable to a specific value. In the case of this snippet, we are setting post_type to page or post or portfolio (our cpt). In simpler terms, this snippet is telling WordPress to find our desired string in any of its posts, pages, or portfolio custom post types and display the results in an archive page. Is that cool or what?

  • 6shares
  • Facebook0
  • Twitter0
  • Pinterest0
  • LinkedIn6
  • Print
  • SMS0

Filed Under: Code Snippet, Programming, WordPress Tagged With: Code Snippet, Programming, WordPress

About Victor M. Font Jr.

Victor M. Font Jr. is an award winning author, entrepreneur, and Senior IT Executive. A Founding Board Member of the North Carolina Executive Roundtable, he has served on the Board of Advisors, of the North Carolina Technology Association, the International Institute of Business Analysis, Association of Information Technology Professionals, Toastmasters International, and the North Carolina Commission for Mental Health, Developmental Disabilities, and Substance Abuse Services. He is author of several books including The Ultimate Guide to the SDLC and Winning With WordPress Basics, and Cybersecurity.

Primary Sidebar

Shopping Cart

Books

  • Ultimate Guide to the SDLC front cover The Ultimate Guide to the SDLC
    Rated 5.00 out of 5
    $74.95
  • Winning With WordPress Basics 2nd Edition Winning With WordPress Basics 2nd Edition $19.95

Recent Articles

  • Modern Scam Defense: How Consumers and Businesses Can Recognize and Stop Email, Phone, and Text Fraud
  • How to Write a PRD So Dense It’s Technically a Novel
  • Top 5 Plugin Names That Scare Our Legal Department
  • When Agile Meets Our 3-Year Waterfall Roadmap: A Love Story
  • Why Our Enterprise Needs 27 Stakeholders to Approve a Button Color Change

Top 10 Article Categories

Best Practice Code Snippet Computers and Internet Genesis How To Leadership Programming Servant Leadership Tutorial WordPress

 
We only use analytical cookies on our website that allow us to recognize and count the number of visitors, but they do not identify you individually. They help us to improve the way our website works. By clicking Accept you, agree to cookies being used in accordance with our Cookie Policy.