@@ -204,6 +204,15 @@ public function get_posts_by_type( $post_type, $slug ) {
204204 ],
205205 ]
206206 );
207+ if ( empty ( $ posts ) && 'page ' === $ post_type ) {
208+ $ default_page_id = $ this ->get_default_page_id_by_type ( $ slug );
209+ if ( 0 !== $ default_page_id ) {
210+ $ post = \get_post ( $ default_page_id );
211+ if ( $ post instanceof \WP_Post ) {
212+ $ posts = [ $ post ];
213+ }
214+ }
215+ }
207216
208217 $ cache [ $ post_type ][ $ slug ] = empty ( $ posts ) ? [] : $ posts ;
209218 return $ cache [ $ post_type ][ $ slug ];
@@ -274,6 +283,91 @@ public function get_default_page_type( $post_type, $post_id ) {
274283 }
275284 }
276285
286+ /**
287+ * Get the default page ID for a page-type.
288+ *
289+ * @param string $page_type The page-type slug.
290+ *
291+ * @return int
292+ */
293+ public function get_default_page_id_by_type ( $ page_type ) {
294+ if ( 'homepage ' === $ page_type ) {
295+ return \get_option ( 'page_on_front ' );
296+ }
297+
298+ // Get posts with a title similar to our query.
299+ $ get_posts_by_title = function ( $ title ) {
300+ global $ wpdb ;
301+ $ posts = $ wpdb ->get_results ( // phpcs:ignore WordPress.DB.DirectDatabaseQuery
302+ $ wpdb ->prepare (
303+ "SELECT ID FROM $ wpdb ->posts WHERE post_title LIKE %s " ,
304+ '% ' . $ wpdb ->esc_like ( $ title ) . '% '
305+ )
306+ );
307+ $ posts_ids = [];
308+ foreach ( $ posts as $ post ) {
309+ $ posts_ids [] = (int ) $ post ->ID ;
310+ }
311+ return $ posts_ids ;
312+ };
313+
314+ $ types_pages = [
315+ 'homepage ' => [ \get_post ( \get_option ( 'page_on_front ' ) ) ],
316+ 'contact ' => $ get_posts_by_title ( __ ( 'Contact ' , 'progress-planner ' ) ),
317+ 'about ' => $ get_posts_by_title ( __ ( 'About ' , 'progress-planner ' ) ),
318+ 'faq ' => array_merge (
319+ $ get_posts_by_title ( __ ( 'FAQ ' , 'progress-planner ' ) ),
320+ $ get_posts_by_title ( __ ( 'Frequently Asked Questions ' , 'progress-planner ' ) ),
321+ ),
322+ ];
323+
324+ $ homepage_id = isset ( $ types_pages ['homepage ' ][0 ] ) ? (int ) $ types_pages ['homepage ' ][0 ]->ID : 0 ;
325+
326+ if ( 'contact ' === $ page_type ) {
327+ $ posts = $ types_pages ['contact ' ];
328+ // Exclude the homepage, about pages and FAQ pages.
329+ $ posts = \array_filter (
330+ $ posts ,
331+ function ( $ post ) use ( $ types_pages , $ homepage_id ) {
332+ return (int ) $ post !== (int ) $ homepage_id
333+ && ! \in_array ( (int ) $ post , $ types_pages ['about ' ], true )
334+ && ! \in_array ( (int ) $ post , $ types_pages ['faq ' ], true );
335+ }
336+ );
337+ return empty ( $ posts ) ? 0 : $ posts [0 ];
338+ }
339+
340+ if ( 'about-us ' === $ page_type ) {
341+ $ posts = $ types_pages ['about ' ];
342+ // Exclude the homepage, contact pages and FAQ pages.
343+ $ posts = \array_filter (
344+ $ posts ,
345+ function ( $ post ) use ( $ types_pages , $ homepage_id ) {
346+ return (int ) $ post !== (int ) $ homepage_id
347+ && ! \in_array ( (int ) $ post , $ types_pages ['contact ' ], true )
348+ && ! \in_array ( (int ) $ post , $ types_pages ['faq ' ], true );
349+ }
350+ );
351+ return empty ( $ posts ) ? 0 : $ posts [0 ];
352+ }
353+
354+ if ( 'faq ' === $ page_type ) {
355+ $ posts = $ types_pages ['faq ' ];
356+ // Exclude the homepage, contact pages and about pages.
357+ $ posts = \array_filter (
358+ $ posts ,
359+ function ( $ post ) use ( $ types_pages , $ homepage_id ) {
360+ return (int ) $ post !== (int ) $ homepage_id
361+ && ! \in_array ( (int ) $ post , $ types_pages ['contact ' ], true )
362+ && ! \in_array ( (int ) $ post , $ types_pages ['about ' ], true );
363+ }
364+ );
365+ return empty ( $ posts ) ? 0 : $ posts [0 ];
366+ }
367+
368+ return 0 ;
369+ }
370+
277371 /**
278372 * Get the taxonomy name.
279373 *
0 commit comments