I’m not entirely clear on what you’re looking for exactly. But in my estimation, maybe this will help you move in the right direction.
I think what you want is a URL rewrite so you can have a URL endpoint that results in your data being displayed. If you work in the right conditional logic, you can avoid a 404 and display an error message instead.
First, you need tell WP that [object-ID] is a query arg. For sake of example, we’ll call that variable $my_object_id
(I’d make sure you have some prefix before “object_id” simply to avoid potential name collisions with other less detailed variable names).
To add your “object ID” as a custom query var you can use in the URL, use the query_vars
filter:
function my_object_id_query_var( $vars ) {
$vars[] = 'my_object_id';
return $vars;
}
add_filter( 'query_vars', 'my_object_id_query_var' );
Now you can pick that up with get_query_var()
:
$object_id = get_query_var( 'my_object_id' );
So now you want to be able to use that in your URL. Use add_rewrite_rule()
to add these to your possible permalink structure:
function my_object_id_rewrite_tag_rule() {
add_rewrite_rule( '^my_object_id/([^/]*)/?', 'index.php?my_object_id=$matches[1]','top' );
}
add_action( 'init', 'my_object_id_rewrite_tag_rule' );
Note that you’ll likely need to refresh your permalink settings – just resave your existing settings.
Then, if your object ID value is “some_id”, you should be able to get it from the url like this:
https://example.com/objects/details/my_object_id/{actual_object_id}/
Note that this answer is likely to take some additional tinkering on your part – it’s not going to fly right out of the box. But I think that adding a rewrite rule is the direction you’re headed. Hope this helps.
Update:
Based on the comments, I think the following may be closer to what you need to use:
// Adding the id var so that WP recognizes it
function SH_insert_query_vars( $vars ) {
array_push( $vars, 'SH_objekt_id' );
return $vars;
}
add_filter( 'query_vars', 'SH_insert_query_vars' );
// define rewrite rule
function SH_objekt_id_rewrite_tag_rule() {
add_rewrite_rule( '^SH_objekt_id/([^/]*)/?', 'index.php?page_id=860&SH_objekt_id=$matches[1]', 'top' );
}
add_action( 'init', 'SH_objekt_id_rewrite_tag_rule' );
The URL then would be:
https://example.com/objects/details/SH_objekt_id/{actual_object_id_value}/
(Note how the query var is used in the URL)
Now in your template, the following should pick up the object ID value:
$object_id = get_query_var( 'SH_objekt_id' );
Update #2 (what ended up working for the OP after working out the details):
User needed to change some details for a specific URL. These are noted in the comments and summarized here for ease of reading the code.
// Adding the id var so that WP recognizes it
function SH_insert_query_vars( $vars ) {
array_push( $vars, 'objekt_id' );
return $vars;
}
add_filter( 'query_vars', 'SH_insert_query_vars' );
// define rewrite rule
function SH_objekt_id_rewrite_tag_rule() {
add_rewrite_rule( '^vermietung/wohnobjekte/details-wohnobjekt/([^/]*)/?', 'index.php?page_id=860&objekt_id=$matches[1]', 'top' );
}
add_action( 'init', 'SH_objekt_id_rewrite_tag_rule' );
Note that now the query var needs to be retrieved as get_query_var( 'objekt_id' )