fetch wordpress posts from wordpress database by php mysql



<?php
<pre><?php
/*
Note : Add your credentials of your server
If you put this file directly on root folder of wordpress and run on the browser it will run automatically
*/
$db_hostname = "localhost";
$db_username = "root";
$db_pwd = "sa";
$db_name = "wpdemolocal";
$db_con = mysql_connect("$db_hostname", "$db_username", "$db_pwd");
if (!$db_con) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("$db_name", $db_con);
if(isset($_REQUEST['submit'])) {

}


$search='';//just a string which displayes what you have searched
$sql= '';
$sql.="SELECT
        wp_posts.ID as postid_final,
        wp_posts.post_author,
        wp_posts.post_title,
        wp_posts.post_status,
        wp_posts.post_modified,
        wp_posts.post_modified_gmt,
        wp_posts.post_type,
        wp_terms.term_id,
        wp_terms.name,
        wp_terms.slug,
        wp_posts.post_content as content
    FROM wp_terms
        INNER JOIN wp_term_taxonomy ON (wp_terms.term_id = wp_term_taxonomy.term_id)
        INNER JOIN wp_term_relationships ON (wp_terms.term_id = wp_term_relationships.term_taxonomy_id)
        INNER JOIN wp_posts ON (wp_term_relationships.object_id = wp_posts.ID)
WHERE ";
$sql.="post_status = 'publish'";// only published post must be shown
$sql.=" and post_type = 'post'";// post type are like post,pages etc

if($_POST['tags']!='')
{
    $sql.=" and wp_terms.name like '%".$_POST['tags']."%'";
    $search.= "TAGS like ".$_POST['tags']."<br />";
}
if($_POST['posttitle']!='')
{
    $sql.=" and wp_posts.post_title like '%".$_POST['posttitle']."%'";
    $search.= "TITLE like ".$_POST['posttitle']."<br />";
}
if($_POST['author']!='')
{
    $sql.=" and wp_posts.post_author = '".$_POST['author']."'";
    $search.= "AUTHOR Eqals ".$_POST['author']."<br />";
}


$sql.="  GROUP BY wp_posts.ID ";

echo "<br />".$sql;
$exec_fetch_setting = mysql_query($sql);
$rows = mysql_num_rows($exec_fetch_setting);
?>
<html>
    <head>
        <style type="text/css">
            .post_content{
                width: 980px;
                text-align: center;
            }
            .post_content_inner{
                margin-bottom: 10px;
                border: 1px solid #cccccc;
            }
            .time_modify{
                float: right;
                color: #000000;
            }
            .post_id{
                float: left;
                 color: #000000;
            }
            .tags{
                 float: left;
                color: #000000;
            }
            .post_prags_search{
                float: left;
                color: #ff0000;
            }
        </style>
    </head>
    <body>
        <div style="text-align:center">
        <form method="POST" action="">
            <table border="1">
                <tr><td>AUTHOR ID</td><td><input type=text'  name='author' id='author' value='' size='60' /></td></tr>
                <tr><td>POST TITLE</td><td><input type=text'  name='posttitle' id='posttitle' value='' size='60' /></td></tr>
                 <tr><td>TAGS</td><td><input type=text'  name='tags' id='tags' value='' size='60' /></td></tr>
                
                <tr><td colspan="2" width="200px">
                   <input type="submit" name="submit"></td>
                </tr>
            </table>
        </form>
        </div>
        <div class="post_prags_search"><?php echo $search; ?></div>
        <br />
        <div class="post_content">
            <?php
            $str = '';
            while ($para = mysql_fetch_assoc($exec_fetch_setting)) {
            $str.='<div class="post_content_inner">';
            $str.='<span class="post_id">'.$para['postid_final']."--".$para['post_title'].'</span><br />';
            $str.='<span class="time_modify">Time modified : '.$para['post_modified'].'</span>';
            $str.='<span class="tags">TAGS : '.$para['name'].'</span>';
            $str.=$para['content'];
            $str.="</div>";
            }
            echo $str;
            ?>
        </div>
    </body>
</html>

</pre>
?>

Share on Facebook




About Pragnesh Karia

Pragnesh Karia, Open Source Enthusiast, Software Professional, Software Developer, Technical Lead ,Magento, Joomla ,Joomla LMS , Moodel LMS ,PHP ,Mysql, Ajax, Javascript, Jquery, Linux, Fan of Open Sources , Annet Technologies , SEO Analyst , Mootools