Using Secondary Title in PHP
Secondary Title has been designed for users with limited coding knowledge. However, if you're a developer or have a little PHP experience, you can make use of Secondary Title's code flexibility.
Work in Progress
This page is still being worked on. Please check back later.
Only Display Posts with Secondary Title
In this example, we want to create a list of all posts and their secondary titles. The built-in function has_secondary_title() will help us with this task. Learn more about this function here.
We set up the PHP logic like this:
<?php
$query = [ 'showposts' => -1 ]; // We want ALL posts
$posts = get_posts($query);Then we use foreach() to loop through our posts and apply the function:
<ul>
<?php
foreach($posts as $post) {
if(has_secondary_title($post->ID)) {
$title = $post->post_title;
$secondary_title = get_secondary_title($post->ID);
?>
<li>
<strong><?php echo $title; ?>: </strong>
<?php echo $secondary_title; ?>
</li>
<?php
}
}
?>
</ul>The Output:
Screenshot coming soon…
Expose a Post's Secondary Title in RESTful API Endpoint
Imagine we're a travel agency with a separate back-end (WordPress) and front-end (Nuxt 4 ♥️). Each of our posts is associated with a location, which is formatted as GPS coordinates (e.g., 13.7563, 100.5018). This enables us to create a Google Maps URL that directs users directly to the specified location. All we need are the post IDs.
Here is how we expose the coordinates to WP's API for our front-end to fetch and process further by making a GET request to /wp-json/post/123/coordinates.
add_action('rest_api_init', function () {
// We register our API endpoint here
}Inside this anonymous function, we register our route:
add_action('rest_api_init', function () {
register_rest_route('post', '/(?P<id>\d+)/coordinates', [
'methods' => 'GET',
'callback' => function(WP_REST_Request $req) {
// Here, we'll do some checks, and if they pass, we return the coordinates
}
]);
});Then let's implement the following checks:
- Does the post exist at all, and is it public?
- Is the Secondary Input field filled with something longer than five characters?
- After attempting to split the coordinates, which are separated by a comma, into an
array, have they made their way successfully into the next array?
<?php
add_action( 'rest_api_init', function () {
if( !function_exists( 'get_secondary_title' ) ) {
return;
}
register_rest_route( 'post', '/(?P<id>\d+)/coordinates', [
'methods' => 'GET',
'callback' => function ( WP_REST_Request $req ) {
$id = $req->get_param( 'id' );
$post = get_post( $id );
if ( empty( $post ) || $post->post_status !== 'publish' || $post->post_type !== 'post' ) {
return new WP_REST_Response( [
'message' => 'Post not found or not published.'
], 404 );
}
$coordinates = get_secondary_title( $post->ID ); // E.g.: 13.7563, 100.5018
if ( empty( $coordinates ) ) {
return new WP_REST_Response( [
'message' => 'Coordinates not found. Did you add any?'
], 404 );
}
$coordinates = explode( ',', trim( $coordinates ) );
$latitude = isset( $coordinates[0] ) ? trim( $coordinates[0] ) : null;
$longitude = isset( $coordinates[1] ) ? trim( $coordinates[1] ) : null;
if ( empty( $latitude ) || empty( $longitude ) ) {
return new WP_REST_Response( [
'message' => 'Coordinates not found. Check your custom fields.'
], 404 );
}
return new WP_REST_Response( [
'post_id' => $post->ID,
'title' => $post->post_title,
'latitude' => ( float ) $latitude,
'longitude' => ( float ) $longitude
], 200 );
},
'permission_callback' => '__return_true',
'args' => [
'id' => [
'validate_callback' => 'is_numeric',
'required' => true
],
],
] );
} );🥳 That was tricky! Of course, this can be improved more and more, but this should give you an example of how to use Secondary Title in… anywhere. Even API routes.
You could now run https://mytravel.com/wp-json/post/15/coordinates and receive the following output in JSON:
[
{
"post_id": 15,
"title": "Bangkok, Thailand",
"latitude": 13.7563,
"longitude": 100.5018
}
]Use JavaScript $fetch() on that endpoint, and you have your own Secondary Title API!