get_permalink() returns empty for certain page IDs

<ul class="stageList2">
    <li><a class="sel">Stage 1</a></li>
    <li><a href="<?php echo get_permalink(1030); ?>">Stage 2</a></li>
    <li><a href="<?php echo get_permalink(1032); ?>">Stage 3</a></li>
    <li><a href="<?php echo get_permalink(1034); ?>">Stage 4</a></li>
    <li><a href="<?php echo get_permalink(1036); ?>">Stage 5</a></li>
    <li><a href="<?php echo get_permalink(1038); ?>">Stage 6</a></li>
    <li><a href="<?php echo get_permalink(1040); ?>">Stage 7</a></li>
    <li><a href="<?php echo get_permalink(1042); ?>">Stage 8</a></li>
    <li><a href="<?php echo get_permalink(1044); ?>">Stage 9a</a></li>
    <li><a href="<?php echo get_permalink(1046); ?>">Stage 9b</a></li>
</ul>

<ul class="stageList2">
    <li><a href="<?php echo get_permalink(1048); ?>">Stage 10</a></li>
    <li><a href="<?php echo get_permalink(1050); ?>">Stage 11</a></li>
    <li><a href="<?php echo get_permalink(1052); ?>">Stage 12</a></li>
    <li><a href="<?php echo get_permalink(1054); ?>">Stage 13</a></li>
    <li><a href="<?php echo get_permalink(1056); ?>">Golf Stage 1</a></li>
    <li><a href="<?php echo get_permalink(1058); ?>">Golf Stage 2</a></li>
    <li><a href="<?php echo get_permalink(1060); ?>">Golf Stage 3</a></li>
    <li><a href="<?php echo get_permalink(1062); ?>">Golf Stage 4</a></li>
    <li><a href="<?php echo get_permalink(1064); ?>">Golf Stage 5</a></li>
    <li><a href="<?php echo get_permalink(1066); ?>">Golf Stage 6</a></li>

I have this code on part of my page. I am trying to get the permalinks for these specific pages, but it returns nothing and the href attribute of the links are empty. If I use other IDs than these ones listed here, the permalinks are returned fine. Also, if I use these IDs to get a permalink on another part of the site, it is returned fine there too. There is something about this specific part of this page template and these specific page IDs that won’t allow me to return the permalinks.

Read More

Has anybody ever experienced anything like this before? Is it some kind of bug that can be fixed?

I have moved my entire WordPress website to a new server, and these permalinks were working completely fine on the other server. I have also disabled all my plugins to see if any of them were causing any errors, but it is still not working.

Cheers,
Justin

Related posts

Leave a Reply

2 comments

  1. When you moved your website to a new server is highly probably that the ids changed depending on how you did the migration and even the configuration of the database in the new host. I always tried to avoid using id’s on the templates. They are not good for reading the code. You can get an id from a title. Something like this:

    get_permalink( get_page_by_title( 'Monthly Events' ) );
    

    The function get_page_by_title will give you the id of the page or post. An even better approach would have been using categories and then iterating over the categories with a loop and query_posts.

    Related to you problem. First, check in the database (if I am not wrong, the table wp_posts) if those numbers (ids) exist. I bet not. If not, you can do an import and export of the database with any database manager (like phpmyadmin), without using the wordpress import/export plugin.

  2. Possible Solution 1: Imagine your workspace. You have your website open to view your changes right? Okay, so when you write code it doesn’t matter how many times your refresh the browser… nothing will show until you save the code.

    Adding a page through your wordpress dashboard is the same thing with respect to your WAMP/XAMP/whatever. I had this issue and I refreshed(right click toolbar icon>”Refresh”. DON’T DO ANYTHING UNTIL ITS DONE!) my WAMP server and the problem went away.

    I can’t guarantee this will work but it’s super simple to do. Hope this works for ya!

    Possible Solution 2: try outputting the get_permalink() (empty parameter). If that prints try href=”<.?php echo get_permalink(); ?>[slug-name]”> [prints htt.p:/[sitename]/ and you just add the [slug-name] or whatever to the end]