Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Okay Now I'm editing old problem because its done but now i've new problem I've knowledge of mysql queries but not wordpress queries I'm doing this with mysql queries and its working well but server is going damn slow i know i'm running about 30 query per page load so is there any way to do it easily with help of wordpress queries or something else ???

 <?php $rss = fetch_feed('http://example.com/feed');
    if (!is_wp_error( $rss ) ) :
        $maxitems = $rss->get_item_quantity(10); 
        $rss_items = $rss->get_items(0, $maxitems); 
    endif;
     if ($maxitems == 0) ;
        else
        foreach ( $rss_items as $item ) :
        $posttitle=$item->get_title();
        $postcontent=$item->get_description();
        $pt=mysql_real_escape_string($posttitle);
        $pc=mysql_real_escape_string($postcontent);
        $result=mysql_query("SELECT post_title FROM wp_posts WHERE post_title='".$pt."'");
        if(mysql_num_rows>0)
        {
        echo "hello";
        mysql_query("UPDATE wp_posts SET post_title='".$pt."',post_content='".$pc."' WHERE post_title='".$pt."'");
        }
        else
        {
        echo "hi";
        mysql_query("INSERT INTO wp_posts (post_title,post_content) VALUES ('".$pt."','".$pc."')");
        }
        endforeach; ?>
share|improve this question

2 Answers 2

up vote 1 down vote accepted
+50

You can do this on background using some ajax on page load, here is an untested example to show the idea.

jQuery(function() {
    jQuery.get('http://domain.tld/remote');
});



<?php
function is_ajax()
{
    return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) and strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
}

function add_rewrite_rules($wp_rewrite)
{
    $new_rules = array
    (
        'remote' => 'index.php?remote=true'
    );

    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}

function add_query_vars($vars)
{
    array_push($vars, 'remote');
    return $vars;
}

function remote_feed()
{
    if (($var = get_query_var('remote')) and is_ajax())
    {
        // get remote feed and insert/update posts
    }
}

add_action('generate_rewrite_rules', 'add_rewrite_rules');
add_filter('query_vars','add_query_vars');
add_action('template_redirect', 'remote_feed');
share|improve this answer

Creation of new post should be done with wp_insert_post() functions.

Also such tasks as fetching remote resources and inserting in database on every page load usually make little sense. What is the point of requesting and inserting same data over and over again?

Update.

Updates will not be much different from inserts for performance. Overwriting everything from feed each time is just horribly inefficient.

There are couple of optimizations to process that would reduce load:

  • run code less often and decouple it from page loads, WP Cron functionality is suitable for this
  • only insert new posts, this means you would need to identify posts already present in database by some criteria (depends on your source data)
  • if you still want to update posts already present, try to determine first if they have been actually changed, for example by comparing of dates
share|improve this answer
    
wel i want if in that feed any new post is comes so only that post can inserted and others updated but that makes too much load in server –  Mohit Bumb Oct 9 '11 at 21:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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