如何在meta_query wordpress中形成动态键值数组

时间:2018-01-04 12:29:14

标签: php wordpress

这是我的元查询

$args = array(
    'post_type'  => 'auto',
    'meta_query' => array(          
        array(
            'key'     => 'a',
            'value'   => 'test', 
        ),
        array(
            'key' => 'b',
            'value'   => 'test', 
        ),
        array(
            'key' => 'c',
            'value'   => '', 
        ),
        array(
            'key' => 'd',
            'value'   => '', 
        ),
        array(
            'key' => 'e',
            'value'   => '', 
        ),
        array(
            'key' => 'f',
            'value'   => '', 
        ),
        array(
            'key' => 'g',
            'value'   => '', 
        ),

    ),
);

我需要动态地形成数组,就像某个键的数组值为null然后它不应该附加到元查询一样。

以上数组键中的示例' c'值为null所以它不应该在元查询中那样明智我有更多的键值对列表,任何帮助都表示赞赏,提前感谢。

2 个答案:

答案 0 :(得分:1)

您可以使用array_filter和回调函数清除每个null数组项,然后将结果数组传递给#数组,然后传递给#

下面的示例将清除数组,如果其值为null,请注意我已将/app/用于多个参数。如果您想使用!/phones...声明,可以详细检查meta_query docs

{
    CComPtr<ID3D11Device> pDevice;
    CComPtr<IDXGIOutputDuplication> pDeskDupl;
    //(...)create devices and duplication interface etc here..
    InitializeDesktopDupl(pDevice, &pDeskDupl, &OutputDuplDesc);
    while(true) //capture loop gist.
    {
    IDXGIResource *pDesktopResource = nullptr;
    DXGI_OUTDUPL_FRAME_INFO FrameInfo;
    RtlZeroMemory(&FrameInfo, sizeof(FrameInfo));
    // Get new frame
    HRESULT hr = pDeskDupl->AcquireNextFrame(
                99,//timeout in ms
                &FrameInfo,
                &pDesktopResource);

            if (hr == DXGI_ERROR_ACCESS_LOST) {
                pDeskDupl->ReleaseFrame();
                pDeskDupl.Release();
                pDesktopResource->Release();

                hr = InitializeDesktopDupl(pDevice, &pDeskDupl, &OutputDuplDesc);
                if(FAILED(hr)){
                 //Check if everything is OK before continuing
                }
            }
     }
}
HRESULT InitializeDesktopDupl(ID3D11Device *pDevice, IDXGIOutputDuplication **ppDesktopDupl, DXGI_OUTDUPL_DESC *pOutputDuplDesc)
{
    *ppDesktopDupl = NULL;
    *pOutputDuplDesc;

    // Get DXGI device
    CComPtr<IDXGIDevice> pDxgiDevice;
    CComPtr<IDXGIOutputDuplication> pDeskDupl = NULL;
    DXGI_OUTDUPL_DESC OutputDuplDesc;

    HRESULT hr = pDevice->QueryInterface(IID_PPV_ARGS(&pDxgiDevice));

    if (FAILED(hr)) { return hr; }
    // Get DXGI adapter
    CComPtr<IDXGIAdapter> pDxgiAdapter;
    hr = pDxgiDevice->GetParent(
        __uuidof(IDXGIAdapter),
        reinterpret_cast<void**>(&pDxgiAdapter));
    pDxgiDevice.Release();
    if (FAILED(hr)) { return hr; }
    // Get output
    CComPtr<IDXGIOutput> pDxgiOutput;
    hr = pDxgiAdapter->EnumOutputs(
        m_DisplayOutput,
        &pDxgiOutput);
    if (FAILED(hr)) { return hr; }
    pDxgiAdapter.Release();

    CComPtr<IDXGIOutput1> pDxgiOutput1;

    hr = pDxgiOutput->QueryInterface(IID_PPV_ARGS(&pDxgiOutput1));
    if (FAILED(hr)) { return hr; }
    pDxgiOutput.Release();

    // Create desktop duplication
    hr = pDxgiOutput1->DuplicateOutput(
        pDevice,
        &pDeskDupl);
    if (FAILED(hr)) { return hr; }
    pDxgiOutput1.Release();

    // Create GUI drawing texture
    pDeskDupl->GetDesc(&OutputDuplDesc);

    pDxgiOutput1.Release();

    *ppDesktopDupl = pDeskDupl;
    (*ppDesktopDupl)->AddRef();
    *pOutputDuplDesc = OutputDuplDesc;
    return hr;
}

答案 1 :(得分:0)

$args['meta_query'] = array_filter($args['meta_query'],function($value){
   return !empty($value['value']);
});

如果是PHP 5.3或更高版本,则使用匿名函数。

{{1}}