custom permalink structure for custom post type based on meta fields and custom taxonomy - wordpress

I have two custom post types. One with the slug artwork and another with the slug research-materials. The permalink structure for an artwork post is http://example.com/artwork/artwork-slug and this is expected, works fine and what I want. On the post.php page for any post that is of the research-materials post type I have a custom field (via ACF) that has the slug of the artwork post that the research materials post is related to and the desired slug for the research materials post. There's also a custom taxonomy set for the research-materials post type which would play a part in the construction of the permalink structure for the research-materials posts. The permalink would look like http://example.com/artwork/related-artwork-slug/research-materials/custom-taxonomy-term/research-materials-slug
Here's the code I have so far but I'm not getting the result I'd expect... instead I'm redirected to the artwork custom post type when I should be redirected tot he research-materials custom post type single page.
Any help appreciated. Thanks in advance.
class RRP{
public static function init(){
// set up custom post types and custom taxonomies
add_action( 'init', 'RRP::build_custom_post_types' );
add_action( 'init', 'RRP::build_custom_taxonomy' );
// set up custom fields
add_action( 'acf/init', 'RRP::register_custom_fields' );
// validate saved value in custom fields
add_filter( 'acf/validate_value', 'RRP::validate_saved_value_in_custom_fields', 10, 4 );
// update related artwork slug
add_filter( 'acf/update_value', 'RRP::update_related_artwork_slug', 10, 3 );
// add the research material type
add_action( 'save_post_research-materials', 'RRP::set_research_material_type' );
// build rewrite rules
add_action( 'init', 'RRP::rewrite_stuff', 10, 0 );
add_filter( 'query_vars', 'RRP::build_query_vars', 10 );
add_filter( 'pre_get_posts', 'RRP::pre_get_posts', 10 );
}
public static function build_custom_post_types(){
$labels = array(
'name' => 'Research Materials',
'singular_name' => 'Research Material',
'add_new' => 'Add New Research Material',
'add_new_item' => 'Add New Research Material',
'edit_item' => 'Edit Research Material',
'new_item' => 'New Research Material',
'view_item' => 'View Research Material',
'search_items' => 'Search Research Materials',
'not_found' => 'No Research Materials found',
'not_found_in_trash' => 'No Research Materials found in Trash',
'parent_item_colon' => 'Parent Research Material:',
'menu_name' => 'Research Materials',
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'description' => 'description',
'taxonomies' => array('research-material-type'),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_admin_bar' => true,
'menu_position' => null,
'menu_icon' => null,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => true,
'capability_type' => 'post',
'supports' => array(
'title', 'editor'
)
);
register_post_type( 'research-materials', $args );
}
public static function build_custom_taxonomy(){
$labels = array(
'name' => 'Research Material Types',
'singular_name' => 'Research Material Type',
'search_items' => 'Search Research Material Types',
'popular_items' => 'Popular Research Material Types',
'all_items' => 'All Research Material Types',
'parent_item' => 'Parent Research Material Type',
'parent_item_colon' => 'Parent Research Material Type',
'edit_item' => 'Edit Research Material Type',
'update_item' => 'Update Research Material Type',
'add_new_item' => 'Add New Research Material Type',
'new_item_name' => 'New Research Material Type Name',
'add_or_remove_items' => 'Add or remove Research Material Types',
'choose_from_most_used' => 'Choose from most used sfmomatheme',
'menu_name' => 'Research Material Type',
);
$args = array(
'labels' => $labels,
'public' => true,
'show_in_nav_menus' => true,
'show_admin_column' => false,
'hierarchical' => false,
'show_tagcloud' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'query_var' => true,
'capabilities' => array(),
);
register_taxonomy( 'research-material-type', array( 'research-materials' ), $args );
$terms = get_terms('research-material-type');
if( empty($terms) ){
wp_insert_term( 'Document', 'research-material-type', array(
'slug' => 'document',
) );
}
}
public static function register_custom_fields(){
acf_add_local_field_group(array(
'key' => 'group_research_materials',
'title' => 'Research Material Custom Fields',
'fields' => array(
array(
'key' => 'field_research_materials_slug',
'type' => 'text',
'name' => 'research_materials_slug',
'label' => 'Research Material Slug',
'required' => 1,
),
array(
'key' => 'field_research_materials_related_artwork',
'type' => 'relationship',
'name' => 'research_materials_related_artwork',
'label' => 'Related Artwork',
'post_type' => array(
'artwork',
),
'max' => 1,
'required' => 1,
),
array(
'key' => 'field_research_materials_related_artwork_slug',
'type' => 'text',
'name' => 'research_materials_related_artwork_slug',
'label' => 'Related Artwork Slug',
'disabled' => 1,
'instructions' => 'Generated based on Related Artwork',
)
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'research-materials',
),
)
),
));
}
public static function validate_saved_value_in_custom_fields($valid, $value, $field, $input){
if( empty($value) && $field['name'] !== 'research_materials_related_artwork_slug' ){
$valid = 'Field must not be empty!';
}
return $valid;
}
public static function set_research_material_type($post_id){
// build main term by default term or by whatever's the first result set
$terms = wp_get_object_terms( strval($post_id), 'research-material-type' );
$default_term = get_term_by( 'slug', 'document', 'research-material-type' );
if( empty($terms) ){
wp_set_object_terms( strval($post_id), $default_term->term_id, 'research-material-type' );
}
else{
wp_set_object_terms( strval($post_id), $terms[0]->term_id, 'research-material-type' );
}
}
public static function build_query_vars($vars){
$vars[] = 'research_materials_related_artwork_slug';
$vars[] = 'research_materials_slug';
$vars[] = 'research_materials_type';
return $vars;
}
public static function update_related_artwork_slug($value, $post_id, $field){
if( $field['name'] === 'research_materials_related_artwork_slug' ){
$value = get_field('research_materials_related_artwork', $post_id)[0]->post_name;
}
return $value;
}
public static function pre_get_posts($query){
// check if the user is requesting an admin page
// or current query is not the main query
if ( is_admin() || !$query->is_main_query() ){
return;
}
$research_materials_related_artwork = get_query_var('research_materials_related_artwork_slug');
$research_materials_slug = get_query_var('research_materials_slug');
$research_materials_type = get_query_var('research_materials_type');
if( !empty($research_materials_related_artwork) && !empty($research_material_slug) && !empty($research_materials_type) ){
$meta_query = $query->get('meta_query');
if( empty($meta_query) ){
$meta_query = array(
'relation' => 'AND',
array(
'key' => 'research_materials_related_artwork_slug',
'value' => $research_materials_related_artwork,
'compare' => '==',
),
array(
'key' => 'research_materials_slug',
'value' => $research_materials_slug,
'compare' => '==',
)
);
}
else{
$meta_query[] = array(
'relation' => 'AND',
array(
'key' => 'research_materials_related_artwork_slug',
'value' => $research_materials_related_artwork,
'compare' => '==',
),
array(
'key' => 'research_materials_slug',
'value' => $research_materials_slug,
'compare' => '==',
)
);
}
$query->set('meta_query', $meta_query);
$tax_query = $query->get('tax_query');
if( empty($tax_query) ){
$tax_query = array(
'relation' => 'AND',
array(
'taxonomy' => 'research-material-type',
'field' => 'slug',
'terms' => array($research_materials_type),
)
);
}
else{
$tax_query[] = array(
'relation' => 'AND',
array(
'taxonomy' => 'research-material-type',
'field' => 'slug',
'terms' => array($research_materials_type),
)
);
}
$query->set('tax_query', $tax_query);
}
return $query;
}
public static function rewrite_stuff(){
add_rewrite_tag( '%research_materials_related_artwork_slug%', '([^&]+)' );
add_rewrite_tag( '%research_materials_slug%', '([^&]+)' );
add_rewrite_rule( '^artwork/([^/]*)/research-materials/([^/]*)/([^/]*)/?', 'index.php?post_type=research-materials&research_materials_related_artwork_slug=$matches[1]&research_materials_slug=$matches[2]&research_materials_type=$matches[3]', 'top' );
}
}
RRP::init();

After much tinkering I figured it out. There were a lot of small things wrong with the above code and I also had to make use of the index_template hook. Here's the final code:
class RRP{
public static function init(){
// set up custom post types and custom taxonomies
add_action( 'init', 'RRP::build_custom_post_types' );
add_action( 'init', 'RRP::build_custom_taxonomy' );
// set up custom fields
add_action( 'acf/init', 'RRP::register_custom_fields' );
// validate saved value in custom fields
add_filter( 'acf/validate_value', 'RRP::validate_saved_value_in_custom_fields', 10, 4 );
// update related artwork slug
add_filter( 'acf/update_value', 'RRP::update_related_artwork_slug', 10, 3 );
// add the research material type
add_action( 'save_post_research-materials', 'RRP::set_research_material_type' );
// build rewrite rules
add_filter( 'index_template', 'RRP::point_artwork_and_artist_to_single_templates' );
add_action( 'init', 'RRP::rewrite_stuff', 0 );
add_filter( 'query_vars', 'RRP::build_query_vars', 10 );
add_filter( 'pre_get_posts', 'RRP::pre_get_posts', 10 );
}
public static function build_custom_post_types(){
$labels = array(
'name' => 'Research Materials',
'singular_name' => 'Research Material',
'add_new' => 'Add New Research Material',
'add_new_item' => 'Add New Research Material',
'edit_item' => 'Edit Research Material',
'new_item' => 'New Research Material',
'view_item' => 'View Research Material',
'search_items' => 'Search Research Materials',
'not_found' => 'No Research Materials found',
'not_found_in_trash' => 'No Research Materials found in Trash',
'parent_item_colon' => 'Parent Research Material:',
'menu_name' => 'Research Materials',
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'description' => 'description',
'taxonomies' => array('research-material-type'),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_admin_bar' => true,
'menu_position' => null,
'menu_icon' => null,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => array(
'slug' => 'research-materials',
'with_front' => false,
),
'capability_type' => 'post',
'supports' => array(
'title', 'editor'
)
);
register_post_type( 'research-materials', $args );
}
public static function build_custom_taxonomy(){
$labels = array(
'name' => 'Research Material Types',
'singular_name' => 'Research Material Type',
'search_items' => 'Search Research Material Types',
'popular_items' => 'Popular Research Material Types',
'all_items' => 'All Research Material Types',
'parent_item' => 'Parent Research Material Type',
'parent_item_colon' => 'Parent Research Material Type',
'edit_item' => 'Edit Research Material Type',
'update_item' => 'Update Research Material Type',
'add_new_item' => 'Add New Research Material Type',
'new_item_name' => 'New Research Material Type Name',
'add_or_remove_items' => 'Add or remove Research Material Types',
'choose_from_most_used' => 'Choose from most used sfmomatheme',
'menu_name' => 'Research Material Type',
);
$args = array(
'labels' => $labels,
'public' => true,
'show_in_nav_menus' => true,
'show_admin_column' => false,
'hierarchical' => false,
'show_tagcloud' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'query_var' => true,
'capabilities' => array(),
);
register_taxonomy( 'research-material-type', array( 'research-materials' ), $args );
$terms = get_terms('research-material-type');
if( empty($terms) ){
wp_insert_term( 'Document', 'research-material-type', array(
'slug' => 'document',
) );
}
}
public static function register_custom_fields(){
acf_add_local_field_group(array(
'key' => 'group_research_materials',
'title' => 'Research Material Custom Fields',
'fields' => array(
array(
'key' => 'field_research_materials_slug',
'type' => 'text',
'name' => 'research_materials_slug',
'label' => 'Research Material Slug',
'required' => 1,
),
array(
'key' => 'field_research_materials_related_artwork',
'type' => 'relationship',
'name' => 'research_materials_related_artwork',
'label' => 'Related Artwork',
'post_type' => array(
'artwork',
),
'max' => 1,
'required' => 1,
),
array(
'key' => 'field_research_materials_related_artwork_slug',
'type' => 'text',
'name' => 'research_materials_related_artwork_slug',
'label' => 'Related Artwork Slug',
'disabled' => 1,
'instructions' => 'Generated based on Related Artwork',
)
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'research-materials',
),
)
),
));
}
public static function validate_saved_value_in_custom_fields($valid, $value, $field, $input){
if( empty($value) && $field['name'] !== 'research_materials_related_artwork_slug' ){
$valid = 'Field must not be empty!';
}
return $valid;
}
public static function set_research_material_type($post_id){
// build main term by default term or by whatever's the first result set
$terms = wp_get_object_terms( strval($post_id), 'research-material-type' );
$default_term = get_term_by( 'slug', 'document', 'research-material-type' );
if( empty($terms) ){
wp_set_object_terms( strval($post_id), $default_term->term_id, 'research-material-type' );
}
else{
wp_set_object_terms( strval($post_id), $terms[0]->term_id, 'research-material-type' );
}
}
public static function build_query_vars($vars){
$vars[] = 'research_materials_related_artwork_slug';
$vars[] = 'research_materials_slug';
$vars[] = 'research_materials_type';
return $vars;
}
public static function update_related_artwork_slug($value, $post_id, $field){
if( $field['name'] === 'research_materials_related_artwork_slug' ){
$value = get_field('research_materials_related_artwork', $post_id)[0]->post_name;
}
return $value;
}
public static function pre_get_posts($query){
// check if the user is requesting an admin page
// or current query is not the main query
if ( is_admin() || !$query->is_main_query() ){
return;
}
$research_materials_related_artwork = $query->get('research_materials_related_artwork_slug');
error_log(print_r($research_materials_related_artwork, true));
$research_materials_slug = $query->get('research_materials_slug');
error_log(print_r($research_materials_slug, true));
$research_materials_type = $query->get('research_materials_type');
error_log(print_r($research_materials_type, true));
if( !empty($research_materials_related_artwork) && !empty($research_materials_slug) && !empty($research_materials_type) ){
$meta_query = array(
'relation' => 'AND',
array(
'key' => 'research_materials_related_artwork_slug',
'value' => $research_materials_related_artwork,
'compare' => '=',
),
array(
'key' => 'research_materials_slug',
'value' => $research_materials_slug,
'compare' => '=',
)
);
$query->set('meta_query', $meta_query);
$tax_query = array(
'relation' => 'AND',
array(
'taxonomy' => 'research-material-type',
'field' => 'slug',
'terms' => array($research_materials_type),
)
);
$query->set('tax_query', $tax_query);
}
return $query;
}
// needed because I also have a CPT called artwork
public static function point_artwork_and_artist_to_single_templates($templates = ''){
global $post;
if( $post->post_type == 'research-materials' ){
$templates = locate_template( 'single-research-materials.php' );
}
return $templates;
}
public static function rewrite_stuff(){
add_rewrite_tag( '%research_materials_related_artwork_slug%', '([^&]+)' );
add_rewrite_tag( '%research_materials_slug%', '([^&]+)' );
add_rewrite_tag( '%research_materials_type%', '([^&]+)' );
add_rewrite_rule( 'artwork\/(.*)\/research-materials\/(.*)\/(.*)\/?$', 'index.php?post_type=research-materials&research_materials_related_artwork_slug=$matches[1]&research_materials_type=$matches[2]&research_materials_slug=$matches[3]', 'top' );
}
}
RRP::init();

Related

Rewrite Custom Post Type(with taxonomies) URL

I have a Custom Post Type with 1 taxonomy and this is the link structure:
mywebsite.com/casos-legal (Show the post in archive-casos-legal.php)
And when I click on any post:
mywebsite.com/casos-legal/lima/this-is-the-post-title
In the case above, "lima" is a term of the taxonomy.
Is there a way to add a path before "casos-legal"? something like this:
mywebsite.com/temas/legal/casos-legal
and:
mywebsite.com/temas/legal/casos-legal/lima/this-is-the-post-title
And much better, change the post slug for just "casos" to get something like this:
mywebsite.com/temas/legal/casos/lima/this-is-the-post-title
Thank in advance!
This is how it looks the CTP
function aprodeh_casos(){
$labels = array(
'name' => 'Casos - legal',
'singular_name' => 'Caso',
'add_new' => 'Agregar',
'all_items' => 'Todos',
'add_new_item' => 'Agregar',
'edit_item' => 'Editar',
'new_item' => 'Nuevo',
'view_item' => 'Ver',
'search_item' => 'Buscar',
'not_found' => 'No se encontraron casos',
'not_found_in_trash' => 'No se encontraron casos en la papelera',
'parent_item_colon' => 'Parent Item',
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'query_var' => true,
'rewrite' => array( 'slug' => '/casos-legal/%categoria_casos%', 'with_front' => false ),
'has_archive' => 'casos-legal',
'capability_type' => 'post',
'hierarchical' => false,
'supports' => array(
'title',
'editor',
'thumbnail',
),
'menu_position' => 5,
'exclude_from_search' => false,
'taxonomies' => array('post_tag'),
);
register_post_type('casos-legal',$args);
} add_action('init','aprodeh_casos');
This is how it looks the Taxonomy:
function aprodeh_casos_taxonomia(){
$labels = array(
'name' => 'Categorías',
'singular_name' => 'Categoría',
'search_items' => 'Buscar categoría',
'all_items' => 'Todas las categorías',
'edit_item' => 'Editar categoría',
'update_item' => 'Actualizar categoría',
'add_new_item' => 'Agregar categoría',
'new_item_name' => 'Nuevo categoría',
'menu_name' => 'Categorías',
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'casos-legal', 'with_front' => false ),
);
register_taxonomy('categoria_casos', array('casos-legal'), $args);
}
add_action('init', 'aprodeh_casos_taxonomia');
And finally, this code in order to get this link structure:
mywebsite.com/casos-legal/lima/this-is-the-post-title
function wpa_casos_permalinks( $post_link, $post ){
if ( is_object( $post ) && $post->post_type == 'casos-legal' ){
$terms = wp_get_object_terms( $post->ID, 'categoria_casos' );
if( $terms ){
return str_replace( '%categoria_casos%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'wpa_casos_permalinks', 1, 2 );
Try updating
'rewrite' => array( 'slug' => '/casos-legal/%categoria_casos%', 'with_front' => false ),
to
'rewrite' => array( 'slug' => '/tema/legal/casos/%categoria_casos%', 'with_front' => false ),

Ordering custom post types

I'm registering a new custom post type like this:
function news_register() {
$labels = array(
'name' => _x('News', 'post type general name'),
'singular_name' => _x('News Item', 'post type singular name'),
'add_new' => _x('Add New News Article', 'resources item'),
'add_new_item' => __('Add New News Article'),
'edit_item' => __('Edit News Article'),
'new_item' => __('New News Article'),
'view_item' => __('View News Article'),
'search_items' => __('Search News Articles'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => 8,
'supports' => array('title','editor', 'excerpt', 'thumbnail', 'excerpt')
);
register_post_type( 'news' , $args );
}
add_action('init', 'news_register');
And I'm then trying to query those post types and order them by title using:
$news = new WP_Query(array(
'post_type' => 'news',
'orderby' => 'name',
'order' => 'DESC'
));
Or:
$args = array( 'post_type' => 'news', 'posts_per_page'=>5, 'orderby'=>'title','order'=>'ASC');
$news = new WP_Query( $args );
Neither of which order by the post title at all. The news posts are test articles that are called 'aaa', 'bbb' and 'ccc.
How can I change my query to get the posts by title order?
EDIT:
I have this code in functions, but removing it doesn't seem to change anything.
add_filter( 'pre_get_posts','change_my_post_order' );
function change_my_post_order( $query ) {
global $wp_query;
if ( is_category() ) {
$query->set( 'orderby', 'title' );
$query->set( 'order', 'asc' );
}
return $query;
}
Adding "'suppress_filters' => true," to the query doesn't seem to make any difference either.
Your pre_get_posts action is wrong. You should make sure that you only target the front end and only the main query. Your action should look like this
add_action( 'pre_get_posts','change_my_post_order' );
function change_my_post_order( $query ) {
if ( !is_admin() && $query->is_main_query() && $query->is_category() ) {
$query->set( 'orderby', 'title' );
$query->set( 'order', 'asc' );
}
}

Wordpress Custom Type permalink with custom Taxonomy slug

I have a custom post type on my site called Homes, with a custom taxonomy called homes-category. They are both working correctly, but I am having trouble with the permalinks. I need the permalink to be homes/homes-category/pagename. I wrote a rewrite function, so the permalink is showing up correctly whenever I go to a homes item, but I get a 404 on the page itself. I am not sure what is causing this and have no ideas on how to fix it. The homes items work fine without the custom taxonomy in the permalink, but not with it. Does anyone have any ideas on how to fix this? I've been searching for days with no luck.
Here is the code for my custom post type:
register_post_type( 'Homes',
array(
'labels' => array(
'name' => __( 'Homes' ),
'singular_name' => __( 'Homes Item' ),
'add_new_item' => __('Add New Homes Item'),
'edit_item' => __('Edit Homes Item'),
'new_item' => __('New Homes Item'),
),
'supports' => array('title', 'thumbnail', 'editor'),
'taxonomies' => array('homes-category'),
'public' => true,
'has_archive' => false,
'show_in_nav_menus' => TRUE,
'show_in_menu' => TRUE,
'rewrite' => array(
'slug' => 'homes/%homes-category%',
'with_front' => true,
'hierarchical' => true,
),
)
);
Here is the code for my custom taxonomy
register_taxonomy(
'homes-category',
'homes',
array(
'hierarchical' => true,
'label' => __( 'Availability Category' ),
'rewrite' => array(
'slug' => 'homes',
'with_front' => false,
),
)
);
And here is the rewrite function
function custom_post_link($post_link, $id = 0)
{
$post = get_post($id);
if(!is_object($post) || $post->post_type != 'homes')
{
return $post_link;
}
$homes = 'misc';
if($terms = wp_get_object_terms($post->ID, 'homes-category'))
{
$client = $terms[0]->slug;
//Replace the query var surrounded by % with the slug of
//the first taxonomy it belongs to.
return str_replace('%homes-category%', $homes, $post_link);
}
//If all else fails, just return the $post_link.
return $post_link;
}
add_filter('post_type_link', 'custom_post_link', 1, 3);
You need to add rewrite_tag for, '%homes-category%'
Even you need to add, custom rewrite_rule, to interpret it correctly.
You can use below code,
add_action('init','wdm_register_post_types');
function wdm_register_post_types()
{
global $wp_rewrite;
register_post_type( 'Homes',
array(
'labels' => array(
'name' => __( 'Homes' ),
'singular_name' => __( 'Homes Item' ),
'add_new_item' => __('Add New Homes Item'),
'edit_item' => __('Edit Homes Item'),
'new_item' => __('New Homes Item'),
),
'supports' => array('title', 'thumbnail', 'editor'),
'taxonomies' => array('homes-category'),
'public' => true,
'has_archive' => false,
'show_in_nav_menus' => TRUE,
'show_in_menu' => TRUE,
'rewrite' => array(
'slug' => 'homes/%homes-cat%',
'with_front' => true,
'hierarchical' => true
),
)
);
register_taxonomy(
'homes-category',
'homes',
array(
'hierarchical' => true,
'label' => __( 'Availability Category' ),
'rewrite' => array(
'slug' => 'homes-category',
'with_front' => true,
),
)
);
$wp_rewrite->add_rewrite_tag( '%homes-cat%', '[a-zA-Z0-9_]');
add_rewrite_rule('^homes/([^/]+)/([^/]+)/?$','index.php?homes=$matches[2]','top');
}
function wp_tuts_filter_post_link( $permalink, $post ) {
if ( false === strpos( $permalink, '%homes-cat%' ) )
return $permalink;
$terms = current(wp_get_post_terms( $post->ID, 'homes-category', array('fields' => 'slugs')));
$permalink = str_replace( '%homes-cat%', $terms , $permalink );
return $permalink;
}
add_filter( 'post_link', 'wp_tuts_filter_post_link' , 10, 2 );
add_filter('post_type_link','wp_tuts_filter_post_link' , 10, 2 );
This will interpret,
http://domain.com/homes/2-bhk/home-1/
properly.
For more details you can also check out this blog post on Creating Custom WordPress Rewrite Rules for Pretty Permalinks

Custom post type won't display, IS homepage & main query

I have a custom post type that worked just fine earlier, but not anymore. I haven't installed any new plugins and my code seems perfectly valid. Other custom fields are showing (default post type), but the ones I'm talking about not anymore.
Here's the code:
// Show posts of 'post', 'homepage_slider' post types on home page
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
function add_my_post_types_to_query( $query ) {
if ( is_home() && $query->is_main_query() )
$query->set( 'post_type', array( 'post', 'homepage_slider' ) );
return $query;
}
As you can guess, homepage_slider is my Custom Post Type. I haven't tweaked it's code one bit, but here it is just for reference:
function homepage_slider() {
$labels = array(
'name' => 'Images',
'singular_name' => 'Image',
'menu_name' => 'Homepage Slider Images',
'parent_item_colon' => 'Parent Image:',
'all_items' => 'All images',
'view_item' => 'View Image',
'add_new_item' => 'Add New Image',
'add_new' => 'Add New',
'edit_item' => 'Edit Image',
'update_item' => 'Update Image',
'search_items' => 'Search Image',
'not_found' => 'Not found',
'not_found_in_trash' => 'Not found in Trash',
);
$args = array(
'label' => 'homepage_slider',
'description' => 'Homepage Slider',
'labels' => $labels,
'supports' => array( 'title', 'thumbnail' ),
// 'taxonomies' => array( 'category', 'post_tag' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'menu_icon' => 'myurl',
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => true,
'publicly_queryable' => true,
'rewrite' => false,
'capability_type' => 'page',
);
register_post_type( 'homepage_slider', $args );
}
// Hook into the 'init' action
add_action( 'init', 'homepage_slider', 0 );
I have queried both the fact that it is indeed the homepage and the main query is indeed running. Really weird error.
Any suggestions?
EDIT: I think something's wrong with the add_my_post_types_to_query function as it's not working with another Custom Post Type either.
Try replacing:
function add_my_post_types_to_query( $query ) {
if ( is_home() && $query->is_main_query() )
$query->set( 'post_type', array( 'post', 'homepage_slider' ) );
return $query;
}
With:
function add_my_post_types_to_query( $query ) {
if ( $query->is_home() && $query->is_main_query() )
$query->set( 'post_type', array( 'post', 'homepage_slider' ) );
return $query;
}
Okay, so here's how I fixed it. Not quite a natural fix, but it did the job.
I simply created a new loop that runs through the custom post type wherever it must be placed and repeat for every other custom field I have on the homepage.
:)

Trying to get both custom post type and posts to show up in tag and category pages

I've built a custom post type, that is set to use the out-of-the-box tags and categories that posts use. However, if I click on a tag or category link, the archive only shows posts with that tag, not my custom post type. I've tried a few ways to fix it but they don't seem to be working. My code is:
// Add Resource Post Type
add_action('init', 'hallam_init');
function hallam_init() {
// set up the labels
$labels = array(
'name' => _x('Resources', 'post type general name'),
'singular_name' => _x('Resource', 'post type singular name'),
'add_new' => _x('Add New', 'resource'),
'add_new_item' => __( 'Add New Resource' ),
'edit_item' => __( 'Edit Resource' ),
'new_item' => __( 'New Resource' ),
'view_item' => __( 'View Resource' ),
'search_items' => __( 'Search Resources' ),
'not_found' => __( 'No resources found' ),
'not_found_in_trash' => __( 'No respources found in Trash' ),
'parent_item_colon' => ''
);
// set up the args
$args = array (
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => array (
'slug' => 'resources'
),
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => 5,
'supports' => array(
'title',
'editor',
'author',
'thumbnail',
'excerpt',
'comments'
),
'taxonomies' => array(
'collection',
'category',
'post_tag'
),
'has_archive' => true
);
register_post_type('ht_resource', $args);
}
// Add Taxonomy
register_taxonomy('collection', 'ht_resource', array(
'hierarchical' => true,
'label' => 'Collections',
'query_var' => true,
'rewrite' => true
));
// Fix the archives
add_filter( 'pre_get_posts', 'add_to_query' );
function add_to_query( $query ) {
// if ( is_home() ) {
if( $query->query_vars['suppress_filters'] ) // TODO check if necessary
return $query;
$supported = $query->get( 'post_type' );
if ( !$supported || $supported == 'post' )
$supported = array( 'post', 'ht_resource' );
elseif ( is_array( $supported ) )
array_push( $supported, 'ht_resource' );
$query->set( 'post_type', $supported );
return $query;
//}
}
Am I missing something obvious?
Can you plz try this by adding on your theme's functions.php? Hope it will work
function query_post_type($query) {
if(is_tag()) {
$query->set('post_type',$post_types=get_post_types('','names'));
return $query;
}
}
add_filter('pre_get_posts', 'query_post_type');
thnx

Resources