0

I'm working on a travel site where there are three parameters: destinations, activities and packages.

Destinations mean the country to travel like: United States, Singapore, Switzerland etc.

Activities are the activities that can be performed inside that country like: hiking, rafting, tour, climbing.

And, package include actual package inside that activities like: hiking in Switzerland, Rafting in Nepal etc.

So, destination may contain number of activities and activities may contain number of packages

I thought of two ways: one creating three post type: destinations, activities and packages and maintaining relationship among them.

Another way is: creating two taxonomies: destinations and activities for single post type package. And maintaining relationship among them.

Which way is better? Is it possible to maintain relationship as my requirement in WordPress? If yes, it'll be nice if someone can give me some clue. Or there is any other solution.

2

@asis, use below on function.php to add the CPT "travel" and tamx.. under it.


function travel() 
{
              $labels = array(
                'name' => _x('travel', 'post type general name'),
                'singular_name' => _x('Project', 'post type singular name'),
                'add_new' => _x('Add New project', 'Project'),
                'add_new_item' => __('Add New Project'),
                'edit_item' => __('Edit Project'),
                'new_item' => __('New Project'),
                'all_items' => __('All travel'),
                'view_item' => __('View Project'),
                'search_items' => __('Search travel'),
                'not_found' =>  __('No travel found'),
                'not_found_in_trash' => __('No travel found in Trash'), 
                'parent_item_colon' => '',
                'menu_name' => __('travel')
              );
              $args = array(
                'labels' => $labels,
                'public' => true,
                'publicly_queryable' => true,
                'show_ui' => true, 
                'show_in_menu' => true, 
                'query_var' => true,
                'rewrite' => true,
                'capability_type' => 'post',
                'has_archive' => true, 
                'hierarchical' => false,
                'menu_position' => 6, 
                'menu_icon' => '',               
                'supports' => array( 'title', 'editor', 'thumbnail','comments')

              ); 
              register_post_type('travel',$args);
}
add_action( 'init', 'travel' );
function travel_taxonomies() 
{
            // Add new taxonomy, make it hierarchical (like categories)
            $labels = array(
                'name'              => _x( 'destinations', 'taxonomy general name' ),
                'singular_name'     => _x( 'destinations', 'taxonomy singular name' ),
                'search_items'      => __( 'Search destinations' ),
                'all_items'         => __( 'All destinations' ),
                'parent_item'       => __( 'Parent destinations' ),
                'parent_item_colon' => __( 'Parent destinations:' ),
                'edit_item'         => __( 'Edit destinations' ),
                'update_item'       => __( 'Update destinations' ),
                'add_new_item'      => __( 'Add New destination' ),
                'new_item_name'     => __( 'New destination Name' ),
                'menu_name'         => __( 'Destinations' ),
            );
            $args = array(
                'hierarchical'      => true,
                'labels'            => $labels,
                'show_ui'           => true,
                'show_admin_column' => true,
                'query_var'         => true,
                'rewrite'           => array( 'slug' => 'destinations' ),
            );
            register_taxonomy( 'destinations', array('travel' ), $args );
            // 2nd taxo.. runs here           
            $labels = array(
                'name'              => _x( 'activities', 'taxonomy general name' ),
                'singular_name'     => _x( 'activities', 'taxonomy singular name' ),
                'search_items'      => __( 'Search activities' ),
                'all_items'         => __( 'All activities' ),
                'parent_item'       => __( 'Parent activities' ),
                'parent_item_colon' => __( 'Parent activities:' ),
                'edit_item'         => __( 'Edit activities' ),
                'update_item'       => __( 'Update activities' ),
                'add_new_item'      => __( 'Add New activity' ),
                'new_item_name'     => __( 'New activity Name' ),
                'menu_name'         => __( 'Activities' ),
            );
            $args = array(
                'hierarchical'      => true,
                'labels'            => $labels,
                'show_ui'           => true,
                'show_admin_column' => true,
                'query_var'         => true,
                'rewrite'           => array( 'slug' => 'activities' ),
            );
            register_taxonomy( 'activities', array('travel' ), $args );      
            // 3rd taxo.. runs here
            $labels = array(
                'name'              => _x( 'packages', 'taxonomy general name' ),
                'singular_name'     => _x( 'packages', 'taxonomy singular name' ),
                'search_items'      => __( 'Search packages' ),
                'all_items'         => __( 'All packages' ),
                'parent_item'       => __( 'Parent packages' ),
                'parent_item_colon' => __( 'Parent packages:' ),
                'edit_item'         => __( 'Edit packages' ),
                'update_item'       => __( 'Update packages' ),
                'add_new_item'      => __( 'Add New package' ),
                'new_item_name'     => __( 'New package Name' ),
                'menu_name'         => __( 'Packages' ),
            );
            $args = array(
                'hierarchical'      => true,
                'labels'            => $labels,
                'show_ui'           => true,
                'show_admin_column' => true,
                'query_var'         => true,
                'rewrite'           => array( 'slug' => 'packages' ),
            );
            register_taxonomy( 'packages', array('travel' ), $args );



}
// hook 
add_action( 'init', 'travel_taxonomies', 0 );
  • Hi, Could you please merge your answers into a single one to keep this post cleaner? Thanks. – Jack Johansson May 27 '17 at 21:21
  • 1
    Sorry for the confusion, i didn't had privileges before.Thanks for the heads up. – Roshan Deshapriya May 28 '17 at 7:15
  • Thanks @rosh8111 for your help. I'm aware of creating post type and taxonomies. Problem is how do I specify one activity is within one destination. Eg. If how to i specify activity: "Tour in Srilanka" is under Destination: "Srilanka" – Ashish May 28 '17 at 9:50
  • use child categories. – Roshan Deshapriya May 29 '17 at 12:11
1

Create 1 post type (packages)and 3 taxonomies under it, each can have whatever categories you want later you can use for hook for filter as per their "destination, activities or whatever".

Let me know if you need assistance creating those.

  • So, i have to create one cpt: packages and two taxonomies ( destinations and activities ) under the cpt. I'll try and let you know. It will be nice if you can explain with some code hints how can I maintain relationship among them as I mentioned in my query. – Ashish May 27 '17 at 12:24
1

Personally, I think it would be easier to manage Packages as the parent taxonomy and Destination / Activities as its children. Wholly depends on how you believe your users will approach your content and how you intend to upload it and display it though.

If, for instance, you were to create a number of posts for each of your packages, then child posts for each of the destinations and activities - you can easily achieve what you want by assigning the parent posts a "Package" category, then adding "Destination" and "Activity" as child categories and assigning them to their respective posts.

This can be done right from the Post Edit screen or the Category tool in the dashboard.

Of course, if you need unique templates these packages (or whatever you decide to use as the parent thing), then I would suggest a custom post type for the parent type, and use either categories or tags for the children.

This would simplify your registering and querying a bit so that instead of registering 3 unique post types, you just have to register one. And when querying post type "Packages" (or whatever), you can easily specify which category of that post type to pull.

Here's an example:

$args = array(
    'post_type' => 'packages',
    'category_name' => 'activities'
);
$query = new WP_Query( $args );

Follow that up with a loop, or specify a single posting that meets the needs of your template, add in some more arguments for good measure if you feel the need, and you're good to go.

Here's a query reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.