How to get post title in WordPress?

How to get post title in WordPress?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

How to Get the All in One SEO Pack Title for Custom Processing

Retrieving the title set in the All in One SEO Pack (AIOSEO) plugin requires bypassing the default WordPress data retrieval functions. By default, get_the_title() returns the content stored in the post_title database column. However, when AIOSEO is active, it often generates a distinct "SEO Title" stored in a meta key to optimize search engine results independently of the post's display title.

To process this specific string—for example, converting Arabic characters to English equivalents in your functions.php—you must explicitly request that meta value rather than relying on the standard getter. The meta key used by AIOSEO is typically _aioseop_title. You can access this value using get_post_meta().

Here is how you modify your existing function:

function arb2en_title($post_id = 0) {
    // Retrieve the post ID if not provided, defaulting to global $post or current ID
    if ($post_id === 0) {
        $post_id = get_the_ID();
    }

    // Get the custom SEO title from AIOSEO plugin
    $seo_title = get_post_meta($post_id, '_aioseop_title', true);

    // Fallback: If the meta key is empty, use the standard post title
    if (empty($seo_title)) {
        $seo_title = get_the_title($post_id);
    }

    $text = $seo_title;

    // Your existing mapping array
    $arb_en_map = array(
        'د' => ']',
        'ج' => '[',
        'Ø­' => 'p',
        'Ø®' => 'o',
        'ه' => 'i',
        'ع' => 'u',
        'غ' => 'y',
        'ف' => 't',
        'ق' => 'r',
        'Ø«' => 'e',
        'ص' => 'w',
        'ض' => 'q',
        'Ø´' => 'a',
        'س' => 's',
        'ي' => 'd',
        'ب' => 'f',
        'ل' => 'g',
        'ا' => 'h',
        'ت' => 'j',
        'ن' => 'k',
        '' => 'l',
        'ك' => ';',
        'Ø·' => '\'',
        'ظ' => '/',
        'ز' => '.',
        'و' => ',',
        'Ø©' => 'm',
        'ى' => 'n',
        'لا' => 'b',
        'ر' => 'v',
        'ؤ' => 'c',
        'Ø¡' => 'x',
        'ئ' => 'z',
        'Ø¥' => 'Y',
        'لإ' => 'T',
        'لأ' => 'G',
        'Ø£' => 'H',
        'لآ' => 'B',
        'Ø¢' => 'N'
    );

    foreach ($arb_en_map as $key => $value) {
        // Use the /e flag for eval only if absolutely necessary, otherwise standard replacement is safer
        // Given the characters are literal, simple replacement works best.
        $text = preg_replace("/$key/", $value, $text);
    }

    return htmlentities($text);
}

Why get_post_meta Is Necessary

The core issue in your original code was attempting to access a non-existent variable named $_aioseop_title. In PHP, this would result in a notice or undefined variable error. WordPress does not expose the AIOSEO title as a global variable; it is stored as post metadata.

The function get_post_meta($post_id, $key, true) retrieves the value associated with the key _aioseop_title for a specific post. The third argument, true, ensures that if multiple values exist (though rare for titles), the function returns a single string rather than an array. This is critical for string manipulation functions like preg_replace.

Handling Fallback Scenarios

A common pitfall when working with SEO plugins is assuming the meta key always contains data. Some themes or configurations might leave the AIOSEO title field empty if you haven't manually entered a custom title, in which case the plugin might inherit the standard post title.

The code above includes a fallback check: if (empty($seo_title)). If the custom SEO title is missing, it gracefully falls back to get_the_title(). This prevents your function from returning an empty string on posts where you haven't customized the AIOSEO title yet. Without this check, your conversion logic would run on an empty string, potentially breaking downstream processes that expect valid text.

Performance Considerations in functions.php

You are running this logic inside a loop or hook within functions.php. If this function is hooked into the_title, it will execute on every page load for every post displayed. While your mapping array is small, the string replacement operations add overhead.

For high-traffic sites, consider caching the result if you are processing these titles for external APIs or logs. However, if this transformation is strictly client-side display logic (e.g., outputting to a JSON feed), ensure you are not double-processing the title. Since functions.php hooks run during the render phase, accessing database meta keys like _aioseop_title happens in real-time.

If you find this slowing down your site, move the heavy lifting to a dedicated service or cache the converted string using a transient if the source text changes infrequently.

Integration Points

When attaching this function to a hook, be mindful of what the_title() expects. If you are replacing the default title output entirely, ensure you are not creating duplicate titles on the front end unless that is your specific architectural intent. For example:

add_filter('the_title', 'arb2en_title');
// This filters the displayed title globally.

Using this filter affects all instances where the_title is used, including RSS feeds and admin lists. If you only need to process the title for a specific export or API endpoint, pass the post ID explicitly instead of filtering the global output.

Our network