douwen4401 2012-06-24 17:31
浏览 51
已采纳

创建自定义WP_Query

I've hit some sort of stumbling bock here, and I can't find my way over it.

I'm trying to craft a custom WP query that searches for posts by country of origin, selecting multiple countries at once.

I have a table that maps countries to regions, so the initial query will search for the selected region, and return all of the countries in that region as an array. For example, if you select Africa, it will return an array of countries within Africa.

if(!empty($_REQUEST['region'])){
     $region = $_REQUEST['region'];

    $regionresult = mysql_query("SELECT * FROM gallery_regions WHERE region='$region'") or die(mysql_error());
    $num_rows = mysql_num_rows($regionresult);

    if (!$regionresult) {
        $message  = 'Invalid query: ' . mysql_error() . "
";
        $message .= 'Whole query: ' . $query;
        die($message);
    }
} else {
     $region = NULL;
}

That part works.

Now, I want to use the returning array to search my WP posts for any that originated from any of the countries in the array. This is the part I can't get to work:

$country_search = array(mysql_fetch_array($regionresult));
$args = array(
    'posts_per_page' => -1,
    'meta_query' => array(
        array(
            'key' => 'Country',
            'value' => '$country_search',
            'compare' => 'IN',
            'category' => 'Gallery'
        )
    )
);
$query = new WP_Query( $args );

I get no results back. In fact, if include echo "$args";, what I get back is simply the word "Array." I also only want it to return results from a specific category.

I'm sure that I'm overlooking something simple. Can anyone help?

Thanks!

ty

  • 写回答

2条回答 默认 最新

  • duanduji2986 2012-06-24 19:09
    关注

    I thought the problem was that there were two array objects, and that this:

    $country_search = array(mysql_fetch_array($regionresult));
    $args = array(
        'posts_per_page' => -1,
        'meta_query' => array(
            array(
                'key' => 'Country',
                'value' => '$country_search',
                'compare' => 'IN',
            )
        )
    );
    $query = new WP_Query( $args );
    

    Should have been this:

    $country_search = array(mysql_fetch_array($regionresult));
    $args = array(
        'posts_per_page' => -1,
        'meta_query' => array(
                'key' => 'Country',
                'value' => $country_search,
                'compare' => 'IN',
        )
    );
    $query = new WP_Query( $args );
    

    But then, instead of returning no posts, it returned all of them. Grumble!

    It tured out to be a combination of issues:

    1. Instead of imploding the $country_search_array, it needs to be added as is to the query.
    2. Since it's an array, we can't use the '=' for the compare value. It needs to be 'IN'

    I couldn't have figured it out without the assistance I received here. Again, thanks!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题