Sql query - how get the permalink from the guid? (3 posts)

  1. antonio nardelli
    Posted 4 years ago #

    Hi, i have this sql query:

    mysql_query("SELECT distinct ID, post_title, guid, wp_posts.ID FROM wp_posts WHERE wp_posts.post_status = 'publish' AND wp_posts.post_type = 'post' order by wp_posts.ID desc");

    then when i try to link the post, the link is in the format http://localhost/?p=1. I'd like the link as the permalink in the blog settings (http://localhost/2011/11/06/hello-world/.

    I read in the forum to use get_permalink () but i don't know how to use it, i get an error!

  2. h0tw1r3
    Posted 4 years ago #

    Using mysql does not allow you to use WordPress functions without extra work (and additional overhead).

    Is it possible for you to use WP_Query instead of calling mysql directly?

    This will do your query exactly, the "wordpress way". You can still access the row variables via the $post variable inside the loop (ie. $post->guid).

    $query = new WP_Query( 'post_status=publish&orderby=ID&order=DESC' );
    while ( $query->have_posts() ): $query->the_post();
        echo get_permalink();
  3. antonio nardelli
    Posted 4 years ago #

    Thank you h0tw1r3, i found a semi-solution using first you rewrited query, then i use sql.

Topic Closed

This topic has been closed to new replies.

About this Topic