new fetch_ids method in Items repository

This commit is contained in:
Leo Germani 2018-03-30 21:16:05 -03:00
parent 651194eaf3
commit 64c282ab13
2 changed files with 36 additions and 1 deletions

View File

@ -295,6 +295,28 @@ class Items extends Repository {
return $this->fetch_output( $wp_query, $output );
}
/**
* fetch items IDs based on WP_Query args
*
* to learn all args accepted in the $args parameter (@see https://developer.wordpress.org/reference/classes/wp_query/)
* You can also use a mapped property, such as name and description, as an argument and it will be mapped to the
* appropriate WP_Query argument
*
* The second paramater specifies from which collections item should be fetched.
* You can pass the Collection ID or object, or an Array of IDs or collection objects
*
* @param array $args WP_Query args || int $args the item id
* @param array $collections Array Entities\Collection || Array int collections IDs || int collection id || Entities\Collection collection object
*
* @return Array array of IDs;
*/
public function fetch_ids( $args = [], $collections = [] ) {
$args['fields'] = 'ids';
return $this->fetch( $args, $collections )->get_posts();
}
public function update( $object, $new_values = null ) {
return $this->insert( $object );

View File

@ -255,6 +255,19 @@ class Items extends TAINACAN_UnitTestCase {
]
], $collection2);
$this->assertEquals(2, $test_query->post_count);
// test fetch ids
$test_query = $Tainacan_Items->fetch_ids([]);
$this->assertTrue( is_array($test_query) );
$this->assertEquals(4, sizeof($test_query) );
$this->assertTrue( is_int($test_query[0]) );
$this->assertTrue( is_int($test_query[1]) );
$this->assertTrue( is_int($test_query[2]) );
$this->assertTrue( is_int($test_query[3]) );
$test_query = $Tainacan_Items->fetch_ids(['title' => 'inexistent']);
$this->assertTrue( is_array($test_query) );
$this->assertEquals(0, sizeof($test_query) );
}
}