Skip to content

Commit 6864899

Browse files
committed
Merge branch 'feature/cpt-by-slug'
2 parents 759607c + 6f42571 commit 6864899

File tree

6 files changed

+152
-6
lines changed

6 files changed

+152
-6
lines changed

README.md

+21-2
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ Returns the following JSON response:
112112
- Author & Author ID
113113
- ACF fields if applicable
114114

115-
### Custom Post Type Post
115+
### Custom Post Type Post by ID
116116
**`better-wp-endpoints/v1/{custom_post_type}/{id}`**
117117
Gets a single custom post type item. Accepts the following parameters:
118118

@@ -131,6 +131,25 @@ Returns the following JSON Response:
131131
- Author & Author ID
132132
- ACF Fields, if applicable
133133

134+
### Custom Post Type Post by Slug
135+
**`better-wp-endpoints/v1/{custom_post_type}/{slug}`**
136+
Gets a single custom post type item. Accepts the following parameters:
137+
138+
- slug
139+
140+
Returns the following JSON Response:
141+
142+
- ID
143+
- slug
144+
- title
145+
- date (ISO 8601)
146+
- post terms
147+
- excerpt
148+
- content
149+
- all possible thumbnail sizes & URLs
150+
- Author & Author ID
151+
- ACF Fields, if applicable
152+
134153
### Menus
135154
**`better-wp-endpoints/v1/menus/{menu-slug}`**
136155
Gets a WordPress Menu by slug. Accepts no parameters.
@@ -141,7 +160,7 @@ Returns the following JSON Response in each item object:
141160
- menu_order
142161
- title
143162
- url
144-
- slug
163+
- slug
145164
- target
146165
- description
147166
- classes (array)

better-wp-endpoints.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Plugin Name: Better WordPress Endpoints
44
Plugin URI: https://github.com/factor1/better-wp-endpoints/
55
Description: Serves up slimmer WordPress Rest API endpoints, with some great enhancements.
6-
Version: 0.1.6
6+
Version: 0.1.7
77
Author: Eric Stout, Factor1 Studios
88
Author URI: https://factor1studios.com/
99
License: GPL3
@@ -95,6 +95,9 @@ private function includes() {
9595
// get custom post type by id
9696
include_once self::$plugin_dir . 'includes/get_cpt_by_id.php';
9797

98+
// get custom post type by slug
99+
include_once self::$plugin_dir . 'includes/get_cpt_by_slug.php';
100+
98101
// get custom post type by id
99102
include_once self::$plugin_dir . 'includes/wp_nav_menus.php';
100103
}

includes/get_cpt_by_slug.php

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
/**
3+
* Get Custom Post Type By Slug
4+
*
5+
* @param array $data Options for the function.
6+
* @return object|null Return post object,
 * or null if none.
7+
* @since 0.0.1
8+
*/
9+
10+
function bwe_build_single_cpt_endpoints_slug() {
11+
if( bwe_get_cpts() ) {
12+
13+
// store what custom post types we have
14+
$cpt_collection = bwe_get_cpts();
15+
16+
foreach ($cpt_collection as $key => $cpt) {
17+
18+
/*
19+
*
20+
* Register Rest API Endpoint
21+
*
22+
*/
23+
register_rest_route( 'better-wp-endpoints/v1', '/'.$cpt.'/(?P<slug>\S+)', array(
24+
'methods' => 'GET',
25+
'callback' => function ( WP_REST_Request $request ) use ($cpt) {
26+
27+
// setup parameters
28+
$post_slug = $request['slug'];
29+
30+
// WP_Query Arguments
31+
$args = array(
32+
'post_type' => $cpt,
33+
'p' => $post_slug,
34+
);
35+
36+
// The Query
37+
$query = new WP_Query( $args );
38+
39+
if( $query->have_posts() ){
40+
41+
// setup post object
42+
$bwe_cpt_post = new stdClass();
43+
44+
while( $query->have_posts() ) {
45+
$query->the_post();
46+
47+
48+
// get post data
49+
$bwe_cpt_post->id = get_the_ID();
50+
$bwe_cpt_post->title = get_the_title();
51+
$bwe_cpt_post->slug = basename(get_permalink());
52+
$bwe_cpt_post->date = get_the_date('c');
53+
$bwe_cpt_post->excerpt = get_the_excerpt();
54+
$bwe_cpt_post->content = apply_filters('the_content', get_the_content());
55+
$bwe_cpt_post->author = esc_html__(get_the_author(), 'text_domain');
56+
$bwe_cpt_post->author_id = get_the_author_meta('ID');
57+
58+
/*
59+
*
60+
* get the terms
61+
*
62+
*/
63+
if( get_object_taxonomies($cpt) ){
64+
$cpt_taxonomies = get_object_taxonomies($cpt, 'names');
65+
66+
$bwe_cpt_post->terms = get_the_terms(get_the_ID(), $cpt_taxonomies);
67+
68+
} else {
69+
$bwe_cpt_post->terms = array();
70+
}
71+
72+
73+
/*
74+
*
75+
* return acf fields if they exist
76+
*
77+
*/
78+
$bwe_cpt_post->acf = bwe_get_acf();
79+
80+
/*
81+
*
82+
* get possible thumbnail sizes and urls
83+
*
84+
*/
85+
$thumbnail_names = get_intermediate_image_sizes();
86+
$bwe_thumbnails = new stdClass();
87+
88+
if( has_post_thumbnail() ){
89+
foreach ($thumbnail_names as $key => $name) {
90+
$bwe_thumbnails->$name = esc_url(get_the_post_thumbnail_url($post->ID, $name));
91+
}
92+
93+
$bwe_cpt_post->media = $bwe_thumbnails;
94+
} else {
95+
$bwe_cpt_post->media = false;
96+
}
97+
98+
}
99+
100+
return $bwe_cpt_post;
101+
} else {
102+
// if no post is found
103+
return array();
104+
}
105+
106+
// reset post data
107+
wp_reset_postdata();
108+
109+
}
110+
));
111+
112+
}
113+
114+
} else {
115+
return array();
116+
}
117+
}
118+
119+
/*
120+
*
121+
* Add action for cpt endpoint building
122+
*
123+
*/
124+
add_action( 'rest_api_init', 'bwe_build_single_cpt_endpoints_slug' );

includes/get_pages.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ function bwe_get_pages( WP_REST_Request $request ) {
125125
'per_page' => array(
126126
'description' => 'Maxiumum number of items to show per page.',
127127
'type' => 'integer',
128-
'validate_callback' => 'is_numeric',
128+
'validate_callback' => function( $v ) { return is_numeric( $v );},
129129
'sanitize_callback' => 'absint',
130130
),
131131
'page' => array(

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "better-wp-endpoints",
3-
"version": "0.1.6",
3+
"version": "0.1.7",
44
"description": "Serves up slimmer WordPress Rest API endpoints.",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)