Wordpress更新自定义字段与条件

时间:2015-10-28 12:25:42

标签: mysql wordpress custom-fields

我想更新(更改)自定义字段(徽标)值,只有当另一个自定义字段具有所述条件时,才会自定义字段" shop"亚马逊"。 我有这个查询

UPDATE `wp_postmeta` SET `meta_key`="logo",`meta_value`="http://www.mywebsite.com/wp-content/uploads/logo-amazon.png" WHERE `meta_key` LIKE 'shop' AND `meta_value` LIKE 'Amazon'

但它不起作用,它在表格中添加了一个新行(我不知道如何说更新......)。能否请你帮忙?提前致谢

1 个答案:

答案 0 :(得分:0)

您的查询中的逻辑错误:您正在尝试更改存储商店价值的元的键和值。

你想要做的是:

UPDATE
  `wp_postmeta` p1
  INNER JOIN `wp_postmeta` p2
  ON p1.post_id = p2.post_id
SET
  p2.meta_value="http://www.mywebsite.com/wp-content/uploads/logo-amazon.png" 
WHERE
  p2.meta_key LIKE 'shop' AND p2.meta_value LIKE 'Amazon' AND
  p1.meta_key = "logo"

它将对更新进行内部联接,以将将shopAmazon设置为p2的帖子ID导入p1,并更新logo中的所有元数据}具有元键$posts = get_posts(array( 'meta_query' => array( array( 'key' => 'shop', 'value' => 'Amazon', ) ) )); foreach($posts as $post) { update_post_meta($post->ID, 'logo', 'http://www.mywebsite.com/wp-content/uploads/logo-amazon.png'); }

虽然考虑到您正在使用Wordpress,但您可以通过更简单的方式执行此操作:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/themes/smoothness/jquery-ui.css" rel="stylesheet" /><script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/jquery-ui.min.js"></script><script>

var qtrix = new Object();

qtrix.$ = jQuery.noConflict();

qtrix.helloWorld = function() {
  alert("Hello world!");
};

qtrix.autoCompleteList = [ ];

function qtrixSetAutoCompleteList(list) {
  qtrixAutoCompleteList = list;
}

qtrix.accentMap = {
    "á": "a",
    "à": "a",
    "ä": "a",

    "é": "e",
    "è": "e",
    "ë": "e",

    "í": "i",
    "ì": "i",
    "ï": "i",

    "ó": "o",
    "ò": "o",
    "ö": "o",

    "ú": "u",
    "ù": "u",
    "ü": "u"
};

qtrix.articles = [
    "the",
    "le",
    "l'",
    "la",
    "die",
    "das",
    "der"
];

qtrix.stripAccents = function(term) {
    var ret = "";
    for (var i = 0; i < term.length; i++) {
    ret += qtrix.accentMap[term.charAt(i)] || term.charAt(i);
    }
    return ret;
};

qtrix.cleanTerm = function(term) {
    term = term.toLowerCase();
    for (var i = 0; i < qtrix.articles.length; i++) {
    if (term.substring(0, qtrix.articles[i].length) == qtrix.articles[i]) {
        return term.substring(qtrix.articles[i].length, term.length)
        .trim();
    }
    }
    return term;
};

qtrix.addAutoComplete = function(id, list) {
    qtrix.$(id).autocomplete({
    source: function(request, response) {
        request.term = qtrix.cleanTerm(request.term);
        var matcher =
        new RegExp(qtrix.$.ui.autocomplete.escapeRegex(request.term), 
               "i");
        response(qtrix.$.grep(list, function(value) {
        value = value.label || value.value || value;
        return matcher.test(value) || 
            matcher.test(qtrix.stripAccents(value));
        }));
    }
    });
};

</script>