WordPress.org

Ready to get started?Download WordPress

Forums

replace the text of a post for Titles and URLs of a Custom post type (1 post)

  1. NatESkiN
    Member
    Posted 2 years ago #

    Hi everyone!
    I have begun to create a function to replace the text of a post for Titles and URLs of a Custom post type. I want to do as a wiki, but automatic.
    Here is my function:

    function replace_content_text_with_post_url($text){
      global $wpdb;
      $results = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'wiki'");
    
      if ( $results ) {
    	foreach ( $results as $post ) {
    	  $title = get_the_title();
    	  $permalink = get_permalink();
    	  $replace[$title] = '<a href="' . $permalink . '">' . $title . '</a>';
    	}
      }
    
      $text = str_replace(array_keys($replace), $replace, $text);
      return $text;
    }
    add_filter('the_content', 'replace_content_text_with_post_url');
    add_filter('the_excerpt', 'replace_content_text_with_post_url');
    add_filter('widget_text', 'replace_content_text_with_post_url');

    I tried without the query of the database and it works, but adding this does not work.
    Does anyone know why?
    Thank you!

Topic Closed

This topic has been closed to new replies.

About this Topic