简体   繁体   English

我想比较帖子标题,如果找不到匹配项,则在wordpress中用php创建新帖子

[英]I want to compare post titles and if no match found then create new post with php in wordpress

I have created following script to creates a new post from CJ web api if post already not exits with sane advertiser name but script does it without checking post exits or not. 如果帖子尚未以健全的广告客户名称退出,但脚本创建了以下脚本以从CJ Web API创建新帖子,但脚本执行了该脚本而不检查帖子是否退出。 ie. 即。 their is no if...else effect, so how can I fix that ? 如果没有其他效果,那就没有了,那我该如何解决呢? I am new to PHP so please ignore my noobsense, thanks. 我是PHP新手,请忽略我的笨拙,谢谢。

      foreach ($data->products[0] as $product) 
    {
        // Sanitize data.
        $price = number_format((float)$product->price, 2, '.', ' ');
        $image = '<a href="'.$product->{'buy-url'}.'"><img src="'.$product->{'image-url'}.
        '" style="float: right"/></a>';
        $pd =  $image.$product->description .'<a href="'.$product->{'buy-url'}.
        '">...For more details and to buy it click here</a>';

        $post_slug = $product->{'advertiser-name'};
        $args=array(
            'cat' => $_GET['cat'],
            'name' => $post_slug,
            'post_type' => 'post',
            'post_status' => 'publish'
                    );
        $my_query = new WP_Query($args);
        if($my_query->have_posts()) {
            echo 'post already exits';
            wp_reset_query();  // Restore global post data stomped by the_post().
}           
            else{

                $p = array('post_title'    => $product->{'advertiser-name'},
  'post_content'  => $pd,
  'post_status'   => 'publish',
  'post_author'   => 1,
  'post_category'  =>array($_GET['cat']));
    $pr = wp_insert_post( $p, $wp_error );
    echo $pr;
    wp_reset_query();  // Restore global post data stomped by the_post().

}

any values come in $my_query->have_posts() ? $my_query->have_posts()是否$my_query->have_posts()任何值?

first check it. 首先检查一下。

try print_r($my_query->have_posts()); 尝试print_r($my_query->have_posts());

then try to do in if(){ ...}else { ....} 然后尝试在if(){ ...}else { ....}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM