visual composer wordpress query for post grid

2019-06-07 05:30发布

问题:

I am using visual composer by http://vc.wpbakery.com/ and when their post grid creator only allows you to use custom post types if you use a query.

I have a simple query working:

post_type=post_mission_trip&post_status=publish&posts_per_page=3

I need to create a more complex one that would do something like this:

$myCompletedResearch = new WP_Query(array(
        'post_type' => 'post_mission_trip',
        'post_status' => 'publish'
        'posts_per_page'=>3,
        'orderby'=>'meta_value_num',
        'meta_key'=>'trip_begin',   
        'order'=>'desc',
        'meta_query'=>array(
        'relation'=>'and',
        array(
            'key'=>'trip_options',
            'value'=>'private_event',
            'compare' => '=='
        ),
        array(
            'key'=>'trip_limit',
            'value'=>'0',
            'compare' => '>'
        )

        )
    ));

Does anyone know how to convert the arrays inside of it to the format that the visual composer would accept.

回答1:

Wordpress WP_query uses parse_str function to parse given attributes, so opposite function is http_build_query that accepts an array as first argument.

echo http_build_query($arr);

 post_type=post_mission_trip&post_status=publish&posts_per_page=3&orderby=meta_value_num&meta_key=trip_begin&order=desc&meta_query%5Brelation%5D=and&meta_query%5B0%5D%5Bkey%5D=trip_options&meta_query%5B0%5D%5Bvalue%5D=private_event&meta_query%5B0%5D%5Bcompare%5D=%3D%3D&meta_query%5B1%5D%5Bkey%5D=trip_limit&meta_query%5B1%5D%5Bvalue%5D=0&meta_query%5B1%5D%5Bcompare%5D=%3E