• Example: If your post title is something like Alex’s book collection you cant get the post array using get_page_by_title().
    Example code:

    $lz_post_name = "Alex's book collection";
    $new_post = get_page_by_title($lz_post_name);
    echo $new_post->ID;

    It should work but it doesn’t work.

    • This topic was modified 5 years, 2 months ago by WPnerdzz.
Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    Works for me on my site. Your code, but different title. There was an issue about apostrophes in titles a while back. Something about titles being double escaped. But it has been fixed now. Are you using the latest WP version? If not, you should update.

    If you are on the latest, try deactivating all plugins. Your code should then work. Then reactivate plugins one by one. When your code again fails, the last activated plugin is the problem.

    If your code fails with no plugins, the problem could be your theme. Switch to a twenty* default theme and temporarily place your test code on a theme template. It will work with a default theme and no plugins.

    Thread Starter WPnerdzz

    (@wpnerdzz)

    Thanks for the reply. My wordpress version was 5. Updated to 5.1 just now. It still doesnt work.
    Here is the code in my theme.
    $lz_version= get_post_meta( $post->ID, 'option_current-release', true );
    a metabox with a number like 1.9.
    $lz_name = get_the_title().' - '.$lz_version;
    it echos something like “Alex’s book collection – 1.9
    $lz_links = get_page_by_title($lz_name, OBJECT, 'release_link');
    release_link is a custom post type. The titles of that CPT are like “Alex’s book collection – 1.9”
    echo get_the_title($lz_links>ID);
    it echos the title of current page “Alex’s book collection”, not desired “Alex’s book collection – 1.9”
    But if I dont add any apostrophe, the code works fine.

    • This reply was modified 5 years, 1 month ago by WPnerdzz.
    • This reply was modified 5 years, 1 month ago by WPnerdzz. Reason: typo
    • This reply was modified 5 years, 1 month ago by WPnerdzz. Reason: additional info
    Thread Starter WPnerdzz

    (@wpnerdzz)

    bump

    Moderator bcworkz

    (@bcworkz)

    If the post title is “Alex’s book collection – 1.9”, what is the point of $lz_name = get_the_title().' - '.$lz_version;? That would result in “Alex’s book collection – 1.9 – 1.9″ and getting a page by that title would fail.

    If the post title is “Alex’s book collection”, and you did $lz_name = get_the_title().' - '.$lz_version;, that would result in “Alex’s book collection – 1.9″ and getting a page by that title would fail because the title is “Alex’s book collection”. Or is there one post of each title?

    In any case, in my experience, get_page_by_title() returns the post requested if it exists, apostrophe or not. If this is not happening for you, it is likely due to interference from your theme or a plugin.

    Thread Starter WPnerdzz

    (@wpnerdzz)

    No no no…

    The title in Post type: book is “Alex’s book collection”
    In single book page, I am trying to post info from another post from different post type: release_link named “Alex’s book collection – 1.9”.
    The title of this post type has a format like <book name> – <version>. I added a meta box in post type: book where I can enter book version (1.9 in this case).

    $lz_name = get_the_title().' - '.$lz_version;
    $lz_links = get_page_by_title($lz_name, OBJECT, 'release_link');

    This works only if there is no inverted commas in book title.

    Thread Starter WPnerdzz

    (@wpnerdzz)

    The post title where I am adding this code is “Alex’s book collection”.
    The other post I am trying to get info from is “Alex’s book collection – 1.9”

    Moderator bcworkz

    (@bcworkz)

    I see now, thanks for explaining. I set up the same scenario on my site. Different custom post type names, but exact same titles. Your code returns the correct post on my site with an apostrophe in the title. I’m unable to replicate the problem, so I can’t investigate the root cause.

    Please install and activate the health-check plugin. The health check item shows up in the left admin menu under Dashboard. Go to the health check screen, then enter troubleshooting mode. This mode will be using the twentynineteen theme, so temporarily place your code inside the Loop of the theme’s single.php template. In troubleshooting mode, view the “Alex’s book collection” post. Your code should now return the appropriate “release_link” post.

    Use the admin bar troubleshooting item to restore your normal theme and plugins, one at a time, testing after each. When the wrong post comes back again, the last activated module is the one causing trouble.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘get_page_by_title() doesnt work if title has an apostrophe ( ‘ )’ is closed to new replies.