I simply want two custom post type meta propertys to be the identifiers for the URL. I implements a solution but it just 404's
registering post type:
final private static function register_post_types ()
{
$labels = array(
'name' => _x( 'Properties', 'post type general name', ' propertystreambootstrap' ),
'singular_name' => _x( 'Property', 'post type singular name', ' propertystreambootstrap' ),
'menu_name' => _x( 'Properties', 'admin menu', ' propertystreambootstrap' ),
'name_admin_bar' => _x( 'Property', 'add new on admin bar', ' propertystreambootstrap' ),
'add_new' => _x( 'Add New', 'property', ' propertystreambootstrap' ),
'add_new_item' => __( 'Add New Property', ' propertystreambootstrap' ),
'new_item' => __( 'New Property', ' propertystreambootstrap' ),
'edit_item' => __( 'Edit Property', ' propertystreambootstrap' ),
'view_item' => __( 'View Property', ' propertystreambootstrap' ),
'all_items' => __( 'All Properties', ' propertystreambootstrap' ),
'search_items' => __( 'Search Properties', ' propertystreambootstrap' ),
'parent_item_colon' => __( 'Parent Properties:', ' propertystreambootstrap' ),
'not_found' => __( 'No properties found.', ' propertystreambootstrap' ),
'not_found_in_trash' => __( 'No properties found in Trash.', ' propertystreambootstrap' )
);
$args = array(
'labels' => $labels,
'description' => __( 'Properties for your website.', ' propertystreambootstrap' ),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_icon' => 'dashicons-building',
'query_var' => true,
//'rewrite' => array( 'slug' => 'malta-property/%department%/%location%/%reference%' ),
'rewrite' => array( 'slug' => 'malta-property/%location%/%reference%'),
'has_archive' => 'about-cool-post-types',
'capability_type' => 'post',
'has_archive' => false,
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title', 'editor', 'excerpt', 'post-thumbnails', 'thumbnail')
);
register_post_type( 'property', $args );
}
Url rewrites:
final public static function url_rewrite()
{
global $wp_rewrite;
$wp_rewrite->add_rewrite_tag('%reference%', '([^&]+)', 'reference=');
$wp_rewrite->add_rewrite_tag('%location%', '([^&]+)', 'location=');
return null;
}
filter function:
final public static function permalink_structure ($permalink, $post, $leavename)
{
if (false !== strpos($permalink, '%reference%')) {
$reference = get_post_meta($post->ID, 'propertystream_agent_ref', true);
$location = wp_get_post_terms($post->ID, 'location');
$location = str_replace('-cat', '', $location[0]->slug);
// die($location);
// $department = wp_get_post_terms($post->ID, 'department');
$rewritecode = array(
'%reference%',
'%location%',
// '%department%',
$post->post_name,
);
$rewritereplace = array(
$reference,
$location,
// $department,
$leavename? $post->post_name : '',
);
$permalink = str_replace($rewritecode, $rewritereplace, $permalink);
}
return $permalink;
}
This all generates the code I want but it just 404's - to get it to work I have to add $leavename = true
in the filter function but that adds the postname to the URL and I do not want that.
I simply want two custom post type meta propertys to be the identifiers for the URL. I implements a solution but it just 404's
registering post type:
final private static function register_post_types ()
{
$labels = array(
'name' => _x( 'Properties', 'post type general name', ' propertystreambootstrap' ),
'singular_name' => _x( 'Property', 'post type singular name', ' propertystreambootstrap' ),
'menu_name' => _x( 'Properties', 'admin menu', ' propertystreambootstrap' ),
'name_admin_bar' => _x( 'Property', 'add new on admin bar', ' propertystreambootstrap' ),
'add_new' => _x( 'Add New', 'property', ' propertystreambootstrap' ),
'add_new_item' => __( 'Add New Property', ' propertystreambootstrap' ),
'new_item' => __( 'New Property', ' propertystreambootstrap' ),
'edit_item' => __( 'Edit Property', ' propertystreambootstrap' ),
'view_item' => __( 'View Property', ' propertystreambootstrap' ),
'all_items' => __( 'All Properties', ' propertystreambootstrap' ),
'search_items' => __( 'Search Properties', ' propertystreambootstrap' ),
'parent_item_colon' => __( 'Parent Properties:', ' propertystreambootstrap' ),
'not_found' => __( 'No properties found.', ' propertystreambootstrap' ),
'not_found_in_trash' => __( 'No properties found in Trash.', ' propertystreambootstrap' )
);
$args = array(
'labels' => $labels,
'description' => __( 'Properties for your website.', ' propertystreambootstrap' ),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_icon' => 'dashicons-building',
'query_var' => true,
//'rewrite' => array( 'slug' => 'malta-property/%department%/%location%/%reference%' ),
'rewrite' => array( 'slug' => 'malta-property/%location%/%reference%'),
'has_archive' => 'about-cool-post-types',
'capability_type' => 'post',
'has_archive' => false,
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title', 'editor', 'excerpt', 'post-thumbnails', 'thumbnail')
);
register_post_type( 'property', $args );
}
Url rewrites:
final public static function url_rewrite()
{
global $wp_rewrite;
$wp_rewrite->add_rewrite_tag('%reference%', '([^&]+)', 'reference=');
$wp_rewrite->add_rewrite_tag('%location%', '([^&]+)', 'location=');
return null;
}
filter function:
final public static function permalink_structure ($permalink, $post, $leavename)
{
if (false !== strpos($permalink, '%reference%')) {
$reference = get_post_meta($post->ID, 'propertystream_agent_ref', true);
$location = wp_get_post_terms($post->ID, 'location');
$location = str_replace('-cat', '', $location[0]->slug);
// die($location);
// $department = wp_get_post_terms($post->ID, 'department');
$rewritecode = array(
'%reference%',
'%location%',
// '%department%',
$post->post_name,
);
$rewritereplace = array(
$reference,
$location,
// $department,
$leavename? $post->post_name : '',
);
$permalink = str_replace($rewritecode, $rewritereplace, $permalink);
}
return $permalink;
}
This all generates the code I want but it just 404's - to get it to work I have to add $leavename = true
in the filter function but that adds the postname to the URL and I do not want that.
1 Answer
Reset to default 1Fixing the 404 error
So it's because of the ([^&]+)
in your add_rewrite_tag()
calls:
$wp_rewrite->add_rewrite_tag('%reference%', '([^&]+)', 'reference=');
//$wp_rewrite->add_rewrite_tag('%location%', '([^&]+)', 'location=');
To fix the error, you should use ([^/]+)
and not ([^&]+)
.
I also intentionally commented out the second line because if the taxonomy's slug is location
, then WordPress should have already added the %location%
rewrite tag.
Removing the post slug from the permalink
You shouldn't remove it from within your permalink_structure()
method, which I assumed is hooked to the post_type_link
filter.
Instead, you can quite easily remove the slug like so: (WordPress by default appends /%<post type slug>%
to the $permastruct['struct']
, so the code below removes that post slug identifier)
// Add this after the post type has been registered (after the register_post_type() call).
global $wp_rewrite;
$permastruct = $wp_rewrite->extra_permastructs['property'];
$permastruct['struct'] = str_replace( '/%property%', '', $permastruct['struct'] );
$wp_rewrite->extra_permastructs['property'] = $permastruct;
And in the permalink_structure()
method, the $rewritecode
should not include the post slug:
$rewritecode = array(
'%reference%',
'%location%',
);
$rewritereplace = array(
$reference,
$location,
);
Locating the correct "property"/post based on the "reference" part in the request/page path
The code below is pretty self-explanatory, but if you've got any questions, let me know:
add_action( 'parse_request', function( $wp ){
// Check if the request/page path begins with /malta-property/<location>/<reference>
if ( preg_match( '#^malta-property/([^/]+)/([^/]+)#', $wp->request, $matches ) ) {
$posts = get_posts( [
'post_type' => 'property',
'meta_key' => 'propertystream_agent_ref',
'meta_value' => $matches[2],
'posts_per_page' => 1,
] );
if ( ! empty( $posts ) ) {
$wp->query_vars['name'] = $posts[0]->post_name;
$wp->query_vars['post_type'] = 'property'; // must set post type
}
}
} );
And remember to flush the rewrite rules — just visit the "Permalink Settings" page.
Also, as mentioned in my comment, I'm assuming each "property" post always has a metadata named propertystream_agent_ref
and that the value is unique.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745306274a4621727.html
propertystream_agent_ref
is unique for each post. Also, iflocation
is a custom taxonomy, you shouldn't need toadd_rewrite_tag()
for the%location%
tag. – Sally CJ Commented Jul 20, 2019 at 7:42%reference%
part). But I wonder if you still need help with this question? (perhaps already answered somewhere else?) – Sally CJ Commented Jul 21, 2019 at 18:37