[wp-trac] [WordPress Trac] #12726: Add get_post_by_*() functions

WordPress Trac noreply at wordpress.org
Fri Oct 11 03:58:48 UTC 2013


#12726: Add get_post_by_*() functions
-------------------------------------+-----------------------------
 Reporter:  mikeschinkel             |       Owner:  sorich87
     Type:  enhancement              |      Status:  accepted
 Priority:  normal                   |   Milestone:  Future Release
Component:  Post Types               |     Version:  3.0
 Severity:  normal                   |  Resolution:
 Keywords:  has-patch needs-testing  |
-------------------------------------+-----------------------------

Comment (by MikeSchinkel):

 Replying to [comment:10 ericlewis]:
 > `get_page_by_path()` and `get_page_by_title()` have both allowed
 $post_type to be passed as an argument since r13774 and r13899. I don't
 see a lot of benefit here, as there aren't any other unique identifying
 fields for posts other than ID and post_name (making `get_page_by_title()`
 problematic in my eyes, since it returns the first post with the given
 title, ignoring multiple posts).

 You make some great points, and 4 years since I posted this ticket I would
 agree.

 Expect for two (2) things; one major and the other nice-to-have.
 Ironically I ran across the former issue within the past month but I'll
 start with explaining the latter:

 1. `get_page_by_path()` has semantics for `post_type=page` so it is harder
 to discover when people are looking for a `get_post_*()` function, and
 confusing for people who do not yet understand that it can be used to
 access other post types.  Offering a `get_post_by()` would thus clean up
 the API in that respect and ''possibly'' allow for deprecating
 `get_page_by_*()` and maybe some other functions.

 2. More importantly there are some things `get_page_by_path()` just cannot
 do. It was designed with `post_type=page` in mind and that's where the
 problems derive. While there may be other page-specific issues most
 notably `get_page_by_path()` cannot lookup a child post if the child post
 type is different from the parent post type. I could elaborate, but
 probably best to let code to the talking ''(see the comments to discover
 the issues):''

 {{{
 /*
  * To test, place this code in a standalone
  * .php page on your web server, i.e. /test.php
  */
 require( __DIR__ . '/wp-load.php' );
 class Limits_of_Get_Page_By_Slug {

   function __construct() {
     add_action( 'init', array( $this, 'init' ) );
   }

   function init() {
     /*
      * Archetype of a defined course, e.g. 'Econ 101'
      */
     register_post_type( 'ex_course', array(
       'label' => 'Courses',
       'public' => true,
     ));
     /*
      * Specific offerings of a defined course, e.g. 'Econ 101, Spring
 2014'
      */
     register_post_type( 'ex_class', array(
       'label' => 'Classes',
       'public' => true,
     ));
   }

    /*
     * This function exposes the problem with get_post_by_slug().
     */
   function expose() {
     $course_id = get_page_by_path( 'econ-101', OBJECT, 'ex_course' );

     if ( ! $course_id ) {
       $course_id = wp_insert_post( array(
         'post_type'   => 'ex_course',
         'post_title'  => 'Econ 101',
         'post_status'  => 'publish',
       ));
     }

     /*
      * This $class_id will always be null.
      */
     $class_id = get_page_by_path( 'econ-101/spring-2014', OBJECT,
 'ex_class' );

     if ( ! $class_id ) {
       /*
        * So this will always be called and duplicates will be added.
        */
       $class_id = wp_insert_post( array(
         'post_type'   => 'ex_class',
         'post_title'  => 'Spring 2014',
         'post_status'  => 'publish',
         'post_parent' => $course_id,
       ));
     }

     $course = get_post( $course_id );
     $class = get_post( $class_id );

     /*
      * Every time this is run you get same $course but a different $class.
      */
     echo "Slug is: " . $course->post_name . '/' . $class->post_name;

   }
 }
 $limits = new Limits_of_Get_Page_By_Slug();
 $limits->expose();
 }}}

 So there's no current WordPress API function that allows for a lookup of a
 child post by slug when the parent post type differs from the child
 ''(nope, neither `get_children()` nor `get_page_children()` do either.)''
 While it is easy to write a function to do this it's not part of the API
 so nobody else it using it, there are no standard hooks to allow others to
 extend it in a common way.

 And there may be a better approach than to create `get_post_by()` but
 mimimally speaking `get_page_by_post()` is not sufficient as is.

--
Ticket URL: <http://core.trac.wordpress.org/ticket/12726#comment:11>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list