I have a somewhat tricky situation. A company’s internal server, running some unknown software (means I have no access whatsoever), exports data to my http server via a custom API. This data gets stored in the same MySQL environment as WordPress. Just a different DB. Meaning DB1 has the WP data, DB2 holds the imported company data.
Now I need to display the imported data in WP. I’m having no problems displaying all data in a kind of overview. I just have a WP page, that in it’s template compiles all the data from the DB and displays a list of all objects.
https://example.com/objects/all-objects
But how would I go about it, displaying one data object? I can’t create a WP page for each data object. These change too fast, to keep up, and that constant adding and deleting would probably confuse the hell out of WP.
If I created an empty page “details”, which would require an ID of what object to display, that might work:
https://example.com/objects/details/[object-ID]
Then I could, in the template of page “details”, read the object-ID from the URL and display the requested data.
Question is:
How can I make sure, this causes no problems with loading the right template, not showing 404, etc ? Is there some function in WP, that would make sure, that “details” gets loaded, no matter what follows the URL-slug?
What is the WordPress way, of accomplishing this?
I keep banging my head against the wall, I guess I’m just missing the right keywords to look for…
Thanks for all hints and help!!!
1 Answer
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' )