Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,16 @@ wp.categories().search( 'news' )...
wp.posts().before( '2013-04-01T00:00:00.000Z' ).after( new Date( 'March 01, 2013' ) )...
```

If you are using the **latest development branch** of the API plugin, there are a few more new query parameter methods you may take advantage of:

```js
// Return ONLY sticky posts
wp.posts().sticky( true )...

// Return NO sticky posts
wp.posts().sticky( false )...
```

#### Paging & Sorting

Convenience methods are also available to set paging & sorting properties like `page`, `per_page` (available as `.perPage()`), `offset`, `order` and `orderby`:
Expand Down
17 changes: 13 additions & 4 deletions lib/mixins/parameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*
* @module filters
*/
var paramSetter = require( '../util/parameter-setter' );

var parameterMixins = {};

Expand Down Expand Up @@ -86,10 +87,18 @@ parameterMixins.parent = function( parentId ) {
* @param {String|Number} post The ID of the post for which to retrieve terms
* @return The request instance (for chaining)
*/
parameterMixins.forPost = function( postId ) {
/* jshint validthis:true */
return this.param( 'post', postId );
};
parameterMixins.forPost = paramSetter( 'post' );

/**
* Specify whether to return only, or to completely exclude, sticky posts
*
* @method sticky
* @chainable
* @param {boolean} sticky A boolean value for whether ONLY sticky posts (true) or
* NO sticky posts (false) should be returned in the query
* @returns The request instance (for chaining)
*/
parameterMixins.sticky = paramSetter( 'sticky' );

// Date Methods
// ============
Expand Down
35 changes: 35 additions & 0 deletions tests/unit/lib/mixins/parameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,39 @@ describe( 'mixins: parameters', function() {

});

describe( 'sticky', function() {

beforeEach(function() {
Req.prototype.sticky = parameterMixins.sticky;
});

it( 'mixin method is defined', function() {
expect( parameterMixins ).to.have.property( 'sticky' );
});

it( 'is a function', function() {
expect( parameterMixins.sticky ).to.be.a( 'function' );
});

it( 'supports chaining', function() {
expect( req.sticky() ).to.equal( req );
});

it( 'has no effect when called with no argument', function() {
var result = req.sticky();
expect( getQueryStr( result ) ).to.equal( '' );
});

it( 'sets the "sticky" query parameter when provided a value', function() {
var result = req.sticky( true );
expect( getQueryStr( result ) ).to.equal( 'sticky=true' );
});

it( 'overwrites previously-set values on subsequent calls', function() {
var result = req.sticky( 1 ).sticky( 0 );
expect( getQueryStr( result ) ).to.equal( 'sticky=0' );
});

});

});