How To Exclude Posts and Pages in WordPress Search

Sometimes we don’t want to display every page and post in search results. Today I like to explain you how to add filter to the search. For this I have added a filter to the search query of WordPress and exclude posts or pages as per my requirement of the search result.

We can exclude posts and pages by their ID, which we will give to an array and so exclude several posts and pages.

In my first code example the ID’s are set in the array. The filter is only working if it is the search is_search and if you are not ! in the backend is_admin.

// search filter
function my_search_filter($query) {

    if ( !$query->is_admin && $query->is_search) {
        $query->set('post__not_in', array(21, 39) ); // id of page or post

    return $query;
add_filter( 'pre_get_posts', 'my_search_filter' );

If you like to exclude the subpage of any page, then you can add below code.

// search filter
function my_search_filter($query) {

    if ( !$query->is_admin && $query->is_search) {
        $pages = array(21, 39); // id of page or post
        // find children to id
        foreach( $pages as $page ):
            $childrens = get_pages( array('child_of' => $page, 'echo' => 0) );
        // add id to array
        for($i = 0; $i < sizeof($childrens); ++$i) {
            $pages[] = $childrens[$i]->ID;
        $query->set('post__not_in', $pages );

    return $query;
add_filter( 'pre_get_posts', 'my_search_filter' );

There are too many possibilities to do this but I think this is a useful post for you.

How to get thumbnail of YouTube video link using YouTube API?

Each YouTube video has 4 generated images. They are predictably formatted as follows:<insert-youtube-video-id-here>/0.jpg<insert-youtube-video-id-here>/1.jpg<insert-youtube-video-id-here>/2.jpg<insert-youtube-video-id-here>/3.jpg

The first one in the list is a full size image and others are thumbnail images. The default thumbnail image (ie. one of 1.jpg, 2.jpg, 3.jpg) is:<insert-youtube-video-id-here>/default.jpg

For the high quality version of the thumbnail use a url similar to this:<insert-youtube-video-id-here>/hqdefault.jpg

There is also a medium quality version of the thumbnail, using a url similar to the HQ:<insert-youtube-video-id-here>/mqdefault.jpg

For the maximum resolution version of the thumbnail use a url similar to this:<insert-youtube-video-id-here>/maxresdefault.jpg

All of the above urls are available over https too. Just change http to https in any of the above urls.

Alternatively, you can use the YouTube API to get thumbnail images.

Let’s Create a Custom Post Type !

Here we shall create a custom post type which will display portfolio. Lets started.

  • Register Your Custom Function.

    Open your function.php of current theme. At the end of the function file, type the following line of code to execute the custom function named create_portfolio during the initialization phase every time a page is generated.

     add_action( 'init', 'create_movie_review' ); 
  • Function Implementation

    Provide an implementation of the create_portfolio function.

     function create_movie_review() {
        register_post_type( 'portfolio',
                'labels' => array(
                    'name' => 'Portfolio',
                    'singular_name' => 'Portfolio',
                    'add_new' => 'Add New',
                    'add_new_item' => 'Add New Portfolio',
                    'edit' => 'Edit',
                    'edit_item' => 'Edit Portfolio',
                    'new_item' => 'New Portfolio',
                    'view' => 'View',
                    'view_item' => 'View Portfolio',
                    'search_items' => 'Search Portfolio',
                    'not_found' => 'No Portfolio found',
                    'not_found_in_trash' => 'No Portfolio found in Trash',
                    'parent' => 'Parent Portfolio'
                'public' => true,
                'menu_position' => 25,
                'supports' => array( 'title', 'editor', 'comments', 'thumbnail', 'custom-fields' ),
                'taxonomies' => array( '' ),
                'menu_icon' => plugins_url( 'images/image.png', __FILE__ ),
                'has_archive' => true



    function does most of the work for us. As soon as it is called it prepares the WordPress environment for a new custom post type including a different sections in wordpress admin. This function takes two arguments: first one is an unique name of the custom post type and the second one an array demonstrating the properties of the new custom post type. Here it’s another array containing the different labels, which indicates the text strings to be displayed in the different sections of the custom post type e.g. ‘name‘ displays the custom post type name in the dashboard, ‘edit‘ and ‘view‘ are displayed in Edit and View buttons respectively.

    In the second arguments:

    • 'public' => true

      determines the visibility of the custom post type both in the admin panel and front end.

    • 'menu_position' => 25

      determines the menu position of the custom post type.

    • 'supports' => array( 'title', 'editor', 'comments', 'thumbnail', 'custom-fields' )

      determines the features of the custom post type which is to be displayed.

    • 'taxonomies' => array( '' )

      creates custom taxonomies.

    • 'menu_icon' => ''

      put the image url which is displaying in admin.

    • 'has_archive' => true

      enables/disable archive of the custom post type.

    For more details please visit the WordPress Codex



  • Now save the function file and go to your wordpress admin.
  • Add new item

    Click on the Add New link to go to the custom post type editor. Provide a portfolio title, a small description and set the featured image.

  • Publish

    Publish the portfolio and click on View Portfolio to view the created portfolio in the browser.

How to create a custom page template in wordpress ?

All custom page templates must start with the following code. It tells WordPress that the file is a custom page template, and sets the name of the template.

<?php /* Template name: My Custom Page Template */ ?> 

Note: you can change Template name as per your requirement. just put the name which you want at the front of the “Template name:”

File Creating

  • In your code editor, create a new file in the theme directory. Call it ‘my_custom_page.php’.
  • In the first line of the file, insert the following code:
     <?php /* Template name: My First Test Page */ ?>  
  • Open the existing page.php file.
  • Select all of the code and copy it and paste the copied code under the code you just inserted.
  • Save the file and login into your site wordpress admin
  • Edit the page you would like to use to this new page template
  • Under ‘Page Attributes’ (usually found on the right-hand side of the editor), select ‘My First Test Page’ from the ‘Template’ drop-down and update/publish your page.

WordPress user data automatically emailed to site owner on registration

Some sites will have additional user data in the registration form, when users register the site owner is notified by email.
For automatic emailing you can use this Code

function new_user_registered($user_id) {
    global $wpdb;
    $admin_email = get_bloginfo('admin_email');

    if (!current_user_can( 'administrator' )){// avoid sending emails when admin is updating user profiles
        $to = $admin_email;
        $subject = 'Hello Admin! You have a new registered user.';
        $message = &quot;The user has registered with the following user meta data:\n&quot;;
        foreach($_POST as $key => $value){
            $message .= $key . ': '. $value . '\n';
        wp_mail( $to, $subject, $message);
add_action( 'user_register', 'new_user_registered');

function yoursite_wp_mail_from($content_type) {
  return '';

function yoursite_wp_mail_from_name($name) {
  return 'From a Nice Name';

Create WordPress Custom Post Type Archive PageTemplates

Post Type Templates

Custom Post Type Templates

  • archive-{post_type}.php
  • single-{post_type}.php
If the custom post type were ‘product’, WordPress would look for archive-product.php to display the archive of posts.
If the custom post type were ‘product’, WordPress would look for single-product.php to display the single or permalink of the post.

If these files are not available in your theme WordPress will look for archive.php and or single.php respectfully. If they are not present it will default to index.php.