I am new with word press stuck in a problem. I have used EXEC-PHP plugin to write php in my text editor and through that I have fetched records from my database. Now I want to share those posts which I have fetched from database on different social sites. For that I have used social 9 plugin. Now I have used the short code in my loop to display the social icons with every post, but when I share the post and open it from any social site it directs me to my word press site but shows no content on it. Here is the code I have been using to fetch data and to share the posts.

global $wpdb;

    $items_per_page = 10;
    $id_post = isset( $_GET['id'] ) ? abs( (int) $_GET['id'] ) : 1;
    $page = isset( $_GET['cpage'] ) ? abs( (int) $_GET['cpage'] ) : 1;
    $chapter = isset( $_GET['chapter_no'] ) ? abs( (int) $_GET['chapter_no'] ) : 1;
    $bookName= isset( $_GET['book'] ) ? urldecode ( $_GET['book'] ) : 1;
    $offset = ( $page * $items_per_page ) - $items_per_page;

    $query = "(Select id, hadith_no, content FROM data WHERE book='$bookName' AND chapter_no='$chapter')";

    $total_query = "SELECT COUNT(*) FROM (${query}) AS combined_table";
    $total = $wpdb->get_var( $total_query );

    $latestposts = $wpdb->get_results("Select id, hadith_no, content FROM data WHERE book='$bookName' AND chapter_no='$chapter' LIMIT $items_per_page OFFSET $offset");

    foreach ($latestposts as $latestpost) {
    echo $latestpost->hadith_no." ".str_repeat('&nbsp;', 140)." <a href='http://localhost:8082/tps/permalinks/?id=$latestpost->id'>Permalink</a>  ".$latestpost->content."<br>";
$text = $latestpost->content;
$words = extractCommonWords($text);
echo  "Tags: <a>" .implode(' , ', array_keys($words)).  "</a>"; 
echo do_shortcode('[Social9_Share]');

Please anyone help me with this, thank you.

  • Let me get this straight: you have included this php code in the content of a post throug the post editor? In any case: how would Social9 know about the posts you have selected? – cjbj Aug 18 '16 at 10:05
  • As i have used the for each loop to display the posts and with every post I get the sharing icons using the short code, so this means that I simply have to click on the share button for any post which i have to share as I am getting the sharing icon with every individual post. – Salik Asad Aug 18 '16 at 11:38
  • And yes I have used this code in the post text editor. – Salik Asad Aug 18 '16 at 11:39

By putting your own php code inside the post editor to select posts and render them, you are evading all mechanisms that WordPress has to do this for you.

By the time you get to do_shortcode('[Social9_Share]') you have echoed a post, but does Social9 know which one? No. As far as Social9 is concerned you want to share the parent post, the one within which you have included the php code. You may run through ten posts inside your foreach loop, but everytime Social9 will echo a button trying to share the parent post.

If that parent post is your homepage, that's what you are sharing.

Moral: NEVER EVER use php in your posts. Use pre_get_posts in combination with is_page to select a certain range of posts on certain pages.

Update: the latter should be included in the functions.php file of your (child) theme.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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