将类别ID或名称添加到where子句$ wpdb

时间:2016-10-17 23:50:06

标签: sql wordpress

这似乎应该是一个简单的答案,但我正在努力解决这个问题。如何在where子句中添加类别ID?我有一堆帖子类别,我想排序类别ID = 4或类别名称=餐馆。下面是我的工作$ wpdb get_results,但在where子句中没有类别ID或名称。我该如何添加?

$(document).ready(function() {

    $("#category").change(function() {
        $.ajax({
            type: "POST",
            url: $link_subcat,
            data: {cat: $(this).val()},
            success: function(data) {
                $("#subcategory").html(data);
            }
        });
    });

    $("input[name=type]").change(function() {
        $.ajax({
            type: "POST",
            url: $link_cat,
            data: {type: $('input[name="type"]:checked').val()},
            success: function(data) {
                $("#category").html(data);
            }
        });
    });

});

1 个答案:

答案 0 :(得分:0)

假设您使用的是“类别”分类,并对术语ID为4或术语名称为“Restaurants”的帖子感兴趣,则查询将如下所示:

...
LEFT JOIN wp_postmeta m4
    ON p.id = m4.post_id AND m4.meta_key = 'bOpenClose'
INNER JOIN wp_term_relationships AS tr
    ON tr.object_id = ID
INNER JOIN wp_term_taxonomy AS tt
    ON tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = 'category'
INNER JOIN wp_terms AS t
    ON tt.term_id = t.term_id  
     WHERE (tt.term_id = 4 OR t.name = 'Restaurants') AND post_type='el2-business' HAVING distance < $thedistance ORDER BY distance");