Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
226 views
in Technique[技术] by (71.8m points)

php - WP_Query按标签搜索(WP_Query searching by tag)

I am in the process of setting up WordPress with WooCommerce, one of the requirements is a bit of integration that takes a person from another website directly to a specific product.

(我正在使用WooCommerce设置WordPress,其中一项要求是集成,将一个人从另一个网站直接带到特定产品。)

I have set the external product references on tags in the products

(我已在产品中的标签上设置了外部产品引用)

an example incoming query would be:

(传入查询的示例为:)

http://localhost/TestSite/search?q=9404

I have written a short plugin that does this, but I can't seem to get it to search products by tag .

(我已经编写了一个简短的插件来执行此操作,但似乎无法通过tag搜索产品 。)

You can see the two lines of $params for example, the top one (currently commented out) works fine when searching by product id, but the bottom one trying to work from tag produces nothing.

(例如,您可以看到$ params的两行,当按产品ID搜索时,最上面的(当前已注释掉)效果很好,但是最下面的试图从tag工作的结果却没有任何效果。)

Can you see where I am going wrong?

(你能看到我要去哪里错吗?)

PS This is my first attempt at a wordpress plugin.

(PS:这是我第一次尝试使用wordpress插件。)

it is potentially inefficient, security compromising or has some glaring issue.

(它可能会导致效率低下,安全性受到损害或存在一些明显的问题。)

I am going to submit it to stack overflow code review, but I need to get this one last bug ironed out!

(我将提交它以进行堆栈溢出代码审阅,但我需要解决这个最后一个错误!)

Thanks for any help you can offer.

(谢谢你尽你所能的帮助。)

//Test Product Id: 11030
//Tag on Test Product: 9404
//http://localhost/TestSite/search?q=9404

function TagLinker() {

    if (isset($_GET['q']))
    {
        $TagNumber = $_GET['q'];

        //create the params array
        //$params = array('post_type' => 'product', 'p' => 11030);
        $params = array('post_type' => 'product', 'tag' => $TagNumber);

        $wc_query = new WP_Query($params);

        if ($wc_query -> have_posts() ) {
            $wc_query -> the_post();

            $product_id = get_the_ID();

            $url = get_permalink( $product_id );

            wp_reset_postdata();

            if ( wp_redirect( $url ) ) {exit;}
        };
    }

}

add_action( 'init', 'TagLinker' );
  ask by EvalKeneval translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...