I got a previous question answered and I tried amending the solution, but now the problem is when there is NOT a space in my custom post URL the tag at position /location/
(in domain/malta-property/department/location/reference_id
) - originally it was just /malta-property/location/reference_id
) but now I've added the /department/
then location will just 404
unless there is a space in the name. It's odd. Note: trans_type
means departments
The potentially interesting bits are the regex maybe? But I can't see anything wrong with them: just for specificity the regex segments are:
$wp_rewrite->add_rewrite_tag('%reference%', '([^/]+)', 'reference=');
$wp_rewrite->add_rewrite_tag('%trans_type%', '([^/]+)', 'trans_type=');
$wp_rewrite->add_rewrite_tag('%location%', '([^/]+)', 'location=');
and
if ( preg_match( '#^malta-property/([^/]+)/([^/]+)/([^/]+)#', $wp->request, $matches ) ) {
the code that builds the custom url for properties is this:
(the register post type args):
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/%trans_type%/%location%/%reference%' ),
//'rewrite' => array( 'slug' => 'malta-property'),
'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 rewrite that gets called on init
and I have flushed permalinks:
final public static function url_rewrite()
{
global $wp_rewrite;
$permastruct = $wp_rewrite->extra_permastructs['property'];
$permastruct['struct'] = str_replace( '/%property%', '', $permastruct['struct'] );
$wp_rewrite->extra_permastructs['property'] = $permastruct;
$wp_rewrite->add_rewrite_tag('%reference%', '([^/]+)', 'reference=');
$wp_rewrite->add_rewrite_tag('%trans_type%', '([^/]+)', 'trans_type=');
$wp_rewrite->add_rewrite_tag('%location%', '([^/]+)', 'location=');
return null;
}
get the product via the URL:
final public static function get_prod_by_ref( $wp )
{
// Check if the request/page path begins with /malta-property/<trans_type>/<location>/<reference>
if ( preg_match( '#^malta-property/([^/]+)/([^/]+)/([^/]+)#', $wp->request, $matches ) ) {
$posts = get_posts( [
'post_type' => 'property',
'meta_key' => 'propertystream_agent_ref',
'meta_value' => $matches[3],
'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
}
}
}
build the custom permalink structure (filtered to post_type_link
):
final public static function permalink_structure ($permalink, $post, $leavename)
{
if (false !== strpos($permalink, '%reference%') && false !== strpos($permalink, '%location%') && false !== strpos($permalink, '%trans_type%')) {
$reference = get_post_meta($post->ID, 'propertystream_agent_ref', true);
$location = wp_get_post_terms($post->ID, 'location');
$type = wp_get_post_terms($post->ID, 'trans_type_id');
//check trans_type_id is set, it it is and is resale the slug should be sales. otherwise just assign it the value
if (!empty($type)) {
if ($type[0]->slug == 'resale') {
$type = 'sales';
}
else
{
$type = $type[0]->slug;
}
}
else {
$type = null;
}
$location = !empty($location) ? $location[0]->name : null;
$rewritecode = array(
'%reference%',
'%location%',
'%trans_type%'
);
$rewritereplace = array(
$reference,
$location,
$type,
);
$permalink = str_replace($rewritecode, $rewritereplace, $permalink);
}
return $permalink;
}
I got a previous question answered and I tried amending the solution, but now the problem is when there is NOT a space in my custom post URL the tag at position /location/
(in domain/malta-property/department/location/reference_id
) - originally it was just /malta-property/location/reference_id
) but now I've added the /department/
then location will just 404
unless there is a space in the name. It's odd. Note: trans_type
means departments
The potentially interesting bits are the regex maybe? But I can't see anything wrong with them: just for specificity the regex segments are:
$wp_rewrite->add_rewrite_tag('%reference%', '([^/]+)', 'reference=');
$wp_rewrite->add_rewrite_tag('%trans_type%', '([^/]+)', 'trans_type=');
$wp_rewrite->add_rewrite_tag('%location%', '([^/]+)', 'location=');
and
if ( preg_match( '#^malta-property/([^/]+)/([^/]+)/([^/]+)#', $wp->request, $matches ) ) {
the code that builds the custom url for properties is this:
(the register post type args):
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/%trans_type%/%location%/%reference%' ),
//'rewrite' => array( 'slug' => 'malta-property'),
'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 rewrite that gets called on init
and I have flushed permalinks:
final public static function url_rewrite()
{
global $wp_rewrite;
$permastruct = $wp_rewrite->extra_permastructs['property'];
$permastruct['struct'] = str_replace( '/%property%', '', $permastruct['struct'] );
$wp_rewrite->extra_permastructs['property'] = $permastruct;
$wp_rewrite->add_rewrite_tag('%reference%', '([^/]+)', 'reference=');
$wp_rewrite->add_rewrite_tag('%trans_type%', '([^/]+)', 'trans_type=');
$wp_rewrite->add_rewrite_tag('%location%', '([^/]+)', 'location=');
return null;
}
get the product via the URL:
final public static function get_prod_by_ref( $wp )
{
// Check if the request/page path begins with /malta-property/<trans_type>/<location>/<reference>
if ( preg_match( '#^malta-property/([^/]+)/([^/]+)/([^/]+)#', $wp->request, $matches ) ) {
$posts = get_posts( [
'post_type' => 'property',
'meta_key' => 'propertystream_agent_ref',
'meta_value' => $matches[3],
'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
}
}
}
build the custom permalink structure (filtered to post_type_link
):
final public static function permalink_structure ($permalink, $post, $leavename)
{
if (false !== strpos($permalink, '%reference%') && false !== strpos($permalink, '%location%') && false !== strpos($permalink, '%trans_type%')) {
$reference = get_post_meta($post->ID, 'propertystream_agent_ref', true);
$location = wp_get_post_terms($post->ID, 'location');
$type = wp_get_post_terms($post->ID, 'trans_type_id');
//check trans_type_id is set, it it is and is resale the slug should be sales. otherwise just assign it the value
if (!empty($type)) {
if ($type[0]->slug == 'resale') {
$type = 'sales';
}
else
{
$type = $type[0]->slug;
}
}
else {
$type = null;
}
$location = !empty($location) ? $location[0]->name : null;
$rewritecode = array(
'%reference%',
'%location%',
'%trans_type%'
);
$rewritereplace = array(
$reference,
$location,
$type,
);
$permalink = str_replace($rewritecode, $rewritereplace, $permalink);
}
return $permalink;
}
Share
Improve this question
edited Jul 23, 2019 at 11:08
nmr
4,5672 gold badges17 silver badges25 bronze badges
asked Jul 23, 2019 at 9:34
EujinksEujinks
1396 bronze badges
0
1 Answer
Reset to default 1So after discussing via chat, the problem is with Ps_Perry_Import_To_Post::wp()
:
final public static function wp()
{
...
if(!empty($_GET['pid']) && empty($_GET['r'])) {
...
wp_redirect('/malta-property/'.$_GET['pid'].'/'.$post_name.'?r=1');
exit();
}
if(strpos($_SERVER['REQUEST_URI'], '/malta-property/') !== false && empty($_GET['r'])) {
...
if (preg_match('/[A-Za-z]/', $arr2[count($arr2)-1]) && preg_match('/[0-9]/', $arr2[count($arr2)-1])) {
// do nothing
return null;
} else {
wp_redirect('/?pid='.$arr1[2]);
exit();
}
}
return null;
}
It's hooked to wp
like so:
add_action('wp', ['Ps_Perry_Import_To_Post', 'wp'], 9);
So you should just remove that (and the above function) and if you need to redirect to the "property" based on the $_GET['pid']
, then try this:
final public static function get_prod_by_ref( $wp )
{
if ( ! empty( $_GET['pid'] ) ) {
$reference = $_GET['pid'];
$redirect = true;
}
// Check if the request/page path begins with /malta-property/<trans_type>/<location>/<reference>
// If yes, cancel above redirect and display the single "property" page.
if ( preg_match( '#^malta-property/([^/]*)/([^/]*)/([^/]+)#', $wp->request, $matches ) ) {
$reference = $matches[3];
$redirect = false;
}
if ( ! empty( $reference ) ) {
$posts = get_posts( [
'post_type' => 'property',
'meta_key' => 'propertystream_agent_ref',
'meta_value' => $reference,
'posts_per_page' => 1,
] );
if ( ! empty( $posts ) ) {
if ( $redirect ) {
wp_redirect( get_permalink( $posts[0] ) );
exit;
}
$wp->query_vars['name'] = $posts[0]->post_name;
$wp->query_vars['post_type'] = 'property'; // must set post type
}
}
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745300531a4621409.html
评论列表(0条)