Support » Fixing WordPress » get_post() return content without permalinks- what to do

  • I use
    echo get_post(1);
    but i get the content with links with page_id (…/?page_id=2). What i should to to get links with permalinks?
    P.S. At the menu links with permalinks.

Viewing 11 replies - 1 through 11 (of 11 total)
  • Hey there,

    Hope you are doing great! 🙂

    I am not sure what exactly you want to achieve. I would suggest you to go through:

    get_post()

    It would be great if you could explain the issue by using a screenshot. Let me know if I am missing something.

    Thanks,
    Archana.

    I have such content in my page (page id 100)

    bla bla bla <a href="/?page_id=1345">click</a> bla bla bla

    In my plugin i have such code

    $product_post = get_post(100);
    echo $product_post->post_content

    Then i activate my plugin i have such html-code

    bla bla bla <a href="/?page_id=1345">click</a> bla bla bla

    but i want

    bla bla bla <a href="/good_result">click</a> bla bla bla

    where “/good_result” is the permalink for page with id 1345.

    P.S. In settings->permalins i have value “Post name”

    Hello there,

    Use get_post($post_id, OBJECT) instead of using just a post id. This will give you an array of post. You can use GUID to create a link to your post.

    Hope this will help you. 🙂

    Thanks,
    Archana.

    But how can i use php code in post’s content (admin-posts-all posts- edit)? It doesn’t work.

    I think it’s a bug

    Hi there,

    I would like to inform you that it doesn’t work this way. You can use the data of the array and perform PHP operations on it. If you wish to perform such PHP operations on post content then you can use custom queries to achieve it.

    See here: https://codex.wordpress.org/Function_Reference/query_posts

    I would suggest you to follow some tutorials on how to customize get_post function for your purpose.

    Thanks,
    Archana.

    Rajan V

    (@rajanit2000)

    @user12289

    I hope there is have no option in WP

    You need to create new plugin. But its great idea..

    function my_update_permalink($content) {
      if ($GLOBALS['post']->post_name == 'debug') {
        return var_export($GLOBALS['post'], TRUE );
      }
    
      //LOGIG HERE
    
      // If link exist get the post id and set new permalink
    
      //LOGIC END
    
      return $content;
    }
    add_filter( 'the_content', 'my_update_permalink' );

    My function

    function linkReplace($post_content){
    
            if(preg_match_all('#href="/\?page_id=(\d*?)"#i', $post_content, $m)){
                $search = array();
                $replace = array();
                foreach ($m[1] as $page_id) {
                    $search[] = '/?page_id='.$page_id;
                    $replace[] = get_permalink($page_id);
                }
                return str_replace($search, $replace, $post_content);
            }
            return $post_content;
        }
    Rajan V

    (@rajanit2000)

    Is it working?

    Yes, it is. Why not?

    Rajan V

    (@rajanit2000)

    Ok great mark as resolved
    Cheers 🙂

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘get_post() return content without permalinks- what to do’ is closed to new replies.