Created
December 22, 2024 20:59
-
-
Save lunule/5e630ae71240b2fc585481ef78662865 to your computer and use it in GitHub Desktop.
[WordPress - `get_page_by_title()` deprecated => Use `WP_Query`] #wp_query #wp #core #get_page_by_title #get #page #by #title https://make.wordpress.org/core/2023/03/06/get_page_by_title-deprecated/
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
| <?php | |
| // Option 1: Use `WP_Query` | |
| $query = new WP_Query( | |
| array( | |
| 'post_type' => 'page', | |
| 'title' => 'Sample Page', | |
| 'post_status' => 'all', | |
| 'posts_per_page' => 1, | |
| 'no_found_rows' => true, | |
| 'ignore_sticky_posts' => true, | |
| 'update_post_term_cache' => false, | |
| 'update_post_meta_cache' => false, | |
| 'orderby' => 'post_date ID', | |
| 'order' => 'ASC', | |
| ) | |
| ); | |
| if ( ! empty( $query->post ) ) { | |
| $page_got_by_title = $query->post; | |
| } else { | |
| $page_got_by_title = null; | |
| } | |
| // -------------------------------------------------------------------------------------------- | |
| // Option 2: Use `WP_Query`'s `get_posts` wrapper | |
| $posts = get_posts( | |
| array( | |
| 'post_type' => 'page', | |
| 'title' => 'Sample Page', | |
| 'post_status' => 'all', | |
| 'numberposts' => 1, | |
| 'update_post_term_cache' => false, | |
| 'update_post_meta_cache' => false, | |
| 'orderby' => 'post_date ID', | |
| 'order' => 'ASC', | |
| ) | |
| ); | |
| if ( ! empty( $posts ) ) { | |
| $page_got_by_title = $posts[0]; | |
| } else { | |
| $page_got_by_title = null; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment