Created
August 15, 2024 22:53
-
-
Save judell/c7a6d7bbd521571986f59585e05e2246 to your computer and use it in GitHub Desktop.
table_wordpress_post.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func listPosts(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) { | |
| conn, err := connect(ctx, d) | |
| if err != nil { | |
| return nil, err | |
| } | |
| plugin.Logger(ctx).Debug("WordPress listPosts author", "author", d.Quals["author"]) | |
| plugin.Logger(ctx).Debug("WordPress listPosts date", "date", d.Quals["date"]) | |
| options := &wordpress.PostListOptions{} | |
| if d.Quals["author"] != nil { | |
| id := d.EqualsQuals["author"].GetInt64Value() | |
| options.Author = []int{int(id)} | |
| } | |
| if d.Quals["date"] != nil { | |
| for _, q := range d.Quals["date"].Quals { | |
| switch q.Operator { | |
| case ">=", ">": | |
| t := q.Value.GetTimestampValue().AsTime() | |
| options.After = &t | |
| case "<=", "<": | |
| t := q.Value.GetTimestampValue().AsTime() | |
| options.Before = &t | |
| } | |
| } | |
| } | |
| plugin.Logger(ctx).Debug("WordPress listPosts API request options", "options", options) | |
| err = paginate( | |
| ctx, // Context for the operation | |
| d, // QueryData containing information about the current query | |
| // This is the ListFunc - an anonymous function that wraps the actual API call | |
| func(ctx context.Context, opts interface{}, perPage, offset int) (interface{}, *wordpress.Response, error) { | |
| // Type assert the generic opts to the specific PostListOptions | |
| // This is necessary because opts is passed as interface{} for flexibility | |
| options := opts.(*wordpress.PostListOptions) | |
| // Set up pagination parameters | |
| options.ListOptions.PerPage = perPage // Number of items per page | |
| options.ListOptions.Offset = offset // Starting position for this page | |
| // Set up sorting parameters | |
| options.ListOptions.OrderBy = "id" // Sort by post ID | |
| options.ListOptions.Order = "asc" // Sort in ascending order | |
| // Make the actual API call using the WordPress SDK | |
| // conn.Posts.List returns ([]wordpress.Post, *wordpress.Response, error) | |
| // But we return it as (interface{}, *wordpress.Response, error) to match ListFunc signature | |
| return conn.Posts.List(ctx, options) | |
| }, | |
| // Pass in the initial options (filters, etc.) set up earlier in the listPosts function | |
| options) | |
| return nil, err | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment