0

I have created a recent post type plugin and it displays recent posts from another website, but the permalinks are not working properly. The permalink does not show the post's URL, it shows the same website URL. Can anyone help me?

Here is my own code:

// db parameters
$db_hostname = 'localhost';
$db_username = 'root';
$db_password = '';
$db_database = 'blog';

// Base folder for the blog. Make SURE there is a slash at the end
$blog_url = 'http://localhost/site/blog/'; 

// Connect to the database
mysql_connect( localhost, $db_username, $db_password );
@mysql_select_db( $db_database ) or die( "Unable to select database" );

// Get data from database
// IMPORTANT, the "LIMIT 5" means how many posts will appear. Change 5 to any whole number.
$query = "Select * FROM wp_posts WHERE post_type='post' AND post_status='publish' 
    ORDER BY id DESC LIMIT 5";

$query_result = mysql_query( $query );
$num_rows = mysql_numrows( $query_result );

// Close database connection
mysql_close();
require_once( ABSPATH . '/blog/wp-load.php' );

$args = array(
    'post_status'    => 'publish',
    'post_type'      => 'post',
    'posts_per_page' => 5
);
$query = new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post();
    ?>
    <h1>"><?php the_title() ?></h1>
    <?php if ( has_post_thumbnail() ) : ?>
        " title="<?php the_title_attribute(); ?>" >
        <?php the_post_thumbnail( 'featured-thumbnail' ) ?>
        <?php the_excerpt() ?>
        <?php the_time( 'M j, Y' ) ?>
        <?php ?>
        <?php
    endif;
endwhile;

1 Answer 1

1

I'm not sure what you're doing with the above script, but here are some random suggestions:

  • Avoid using the root user, for security reasons create another database user with less powers.
  • Avoid using deprecated mysql_* calls.
  • Use WP_Query() or get_posts() instead of hardcoded SQL queries.
  • Use global $wpdb object if you really must use hardcoded SQL queries.
  • Remove the <?php ... ?> spam
  • Try doing this within WordPress itself, instead of creating an extenral script.php file.
  • You don't seem to be using the $blog_url, $query_result, $num_rows, ... variables.
  • There are other ways like WP REST API, XML-RPC, RSS, JSON
2
  • Can you give me some Example for this....... Commented Nov 9, 2015 at 5:25
  • The fetch_feed() is handy to fetch external feeds. @VikasAgarwal
    – birgire
    Commented Nov 9, 2015 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.