• US: +1 347 223 5853   UK: +44 20 8 1234 790
  • Live Chat

Building an advanced wordpress search using wp-query

  •  admin
  •  Jun 10, 2016

https://www.smashingmagazine.com/2016/03/advanced-wordpress-search-with-wp_query/

Many websites keep up a database of special posts as hotel booking sites, immo websites and others. They all start from a special post type different from an ordinary blog post. To this new type you can couple a number of properties as location, room properties, price a.s.o. You don’t see this type of posts in a common ready to use WordPress installation. But it is what you get with some plugins more specificly for real estate, hotel booking …

Let’s make a website with this kind of custom type and call it “My Real Estate”. You can visit it here.

Before starting the search function itself, we will make a new post type and call this for instance: property.

We find the base code to create a new post here in the WP codex.

Adapted for our aim you get next code that must be put in functions.php:

add_action( 'init', 'create_post_type' );
function create_post_type() {
  register_post_type( 'property',
    array(
      'labels' => array(
        'name' => __( 'Properties' ),
        'singular_name' => __( 'Property' )
      ),
      'public' => true,
      'has_archive' => true,
    )
  );
}

<!–

function my_custom_post_property() {
$labels = array(
    'name'               => _x( 'Properties', 'post type general name' ),
    'singular_name'      => _x( 'Property', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'property' ),
    'add_new_item'       => __( 'Add New Property' ),
    'edit_item'          => __( 'Edit Property' ),
    'new_item'           => __( 'New Property' ),
    'all_items'          => __( 'All Properties' ),
    'view_item'          => __( 'View Property' ),
    'search_items'       => __( 'Search Properties' ),
    'not_found'          => __( 'No properties found' ),
    'not_found_in_trash' => __( 'No properties found in the Trash' ), 
    'parent_item_colon'  => '',
    'menu_name'          => 'Properties'
  );

$args = array(
    'labels'        => $labels,
    'description'   => 'Holds our properties and product specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'custom-fields', 'sticky-post', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  
register_post_type( 'property', $args ); 
}
add_action( 'init', 'my_custom_post_property' );

And

function my_taxonomies_property() {
  $labels = array(
    'name'              => _x( 'Property Categories', 'taxonomy general name' ),
    'singular_name'     => _x( 'Property Category', 'taxonomy singular name' ),
    'search_items'      => __( 'Search Property Categories' ),
    'all_items'         => __( 'All Property Categories' ),
    'parent_item'       => __( 'Parent Property Category' ),
    'parent_item_colon' => __( 'Parent Property Category:' ),
    'edit_item'         => __( 'Edit Property Category' ), 
    'update_item'       => __( 'Update Property Category' ),
    'add_new_item'      => __( 'Add New Property Category' ),
    'new_item_name'     => __( 'New Property Category' ),
    'menu_name'         => __( 'Property Categories' ),
  );
  $args = array(
    'labels' => $labels,
    'hierarchical' => true,
  );
  register_taxonomy( 'property_category', 'property', $args );
}
add_action( 'init', 'my_taxonomies_property', 0 );

–>

admin

Other posts by

Advanced analytics tool.