• Hello Everyone!

    I have the following code:
    What the code below does is, whenever the user publishes a new custom post, a short code is automatically inserted in the editor area of the post. i.e.
    [custom-trick id=234]
    where 234 is a an example custom post id

    //-------------------------------------------
    function update_post_content( $ID, $post ) {
        if (get_post_meta($ID, "first_time_published", false)) {
            return;
        }
          $my_post = array(
              'ID'           => $ID,
              'post_content' => "[custom-trick id=$ID]",
          );
        update_post_meta($ID, "first_time_published", true);
          wp_update_post($my_post);
    }
    add_action( 'publish_custom-trick', 'update_post_content', 10, 2 );
    //-----------------------------------------------------

    What I want to do is modify above code in this way:

    START:
    When the custom post is published, the code searches for all published custom posts with identical “Title”.

    MATCH NOT FOUND:
    If it does not find any match, then the code simply does what the above code is already doing i.e. update the content of the custom post with [custom-trick id=234]

    MATCH FOUND:
    If it finds a match, then the code simply does what the above code is already doing PLUS it also adds another shortcode
    [custom-trick id=234][custom-trick id=342]
    where id=234 is the id of the matched post and id=342 is id of the current custom post that just got published.

    • This topic was modified 7 years, 8 months ago by bcworkz. Reason: code fixed
Viewing 11 replies - 1 through 11 (of 11 total)
  • Moderator bcworkz

    (@bcworkz)

    Use the WP_Query class to query for any posts matching your title and post type criteria. Set the posts_per_page argument to 1. There’s no need to waste time getting more posts when one will do.

    It’s not clear in the linked page, but if you looked at the class declaration, you would see that the query result is in $the_query->posts. If a post is found, its ID would be $the_query->posts[0]->ID

    Probably the simplest logic would be (in pseudo code):

    if first_time_published():
    $content = "[custom-trick id=$ID]"
    $the_query = do_custom_query($post->post_title)
    if $the_query->post_count == 1 then $content .= "[custom-trick id={$the_query->posts[0]->ID}]"
    update_first_time_published()
    endif;
    Thread Starter johnkelton

    (@johnkelton)

    Thanks a lot for your help. It does point me to right direction; since I am new to php I am finding it hard to convert the pseudo code. I read the document you pointed me to but I am still in no clue land. Any advance help is much appreciated!

    Moderator bcworkz

    (@bcworkz)

    Well, most of my pseudo code reiterates your code, I was mainly trying to show the preferred logic, which gets obfuscated with functional code. The logic is simpler than what you described in your OP.

    This replaces the do custom query line and the subsequent conditional:

    $args = array(
      'title'=> $post->post_title,
      'post_type'=>'my_post_type',
      'posts_per_page'=> 1,
    );
    $the_query = new WP_Query( $args );
    if ( 1 == $the_query->post_count ) {
      $content .= "[custom-trick id={$the_query->posts[0]->ID}]";
    }

    Be sure to replace ‘my_post_type’ with your actual custom post type slug.

    Thread Starter johnkelton

    (@johnkelton)

    Ok, so I will add this to my existing code?
    LIke:

    function update_post_content( $ID, $post ) {
        if (get_post_meta($ID, "first_time_published", false)) {
            return;
        }
          $my_post = array(
              'ID'           => $ID,
              'post_content' => "[custom-trick id=$ID]",
          );
        update_post_meta($ID, "first_time_published", true);
          wp_update_post($my_post);
    
    $args = array(
      'title'=> $post->post_title,
      'post_type'=>'my_post_type',
      'posts_per_page'=> 1,
    );
    $the_query = new WP_Query( $args );
    if ( 1 == $the_query->post_count ) {
      $content .= "[custom-trick id={$the_query->posts[0]->ID}]";
    }
    
    }
    add_action( 'publish_custom-trick', 'update_post_content', 10, 2 );
    Thread Starter johnkelton

    (@johnkelton)

    If your snippet is all what I need then I dont see how will it just do:

    [custom-trick id=234] for a custom-post with unique title

    VS

    [custom-trick id=234][custom-trick id=342] for a custom post-post with duplicate title

    Moderator bcworkz

    (@bcworkz)

    Heh, what we have here is a failure to communicate 🙂 Totally my fault for making bad assumptions. Bear with me.

    This should work (once the correct post type is in place in $args), but is untested:

    function update_post_content( $ID, $post ) {
        if ( get_post_meta($ID, "first_time_published", false )) {
            return;
        }
    
        $content = "[custom-trick id=$ID]";
        $args = array(
          'title'=> $post->post_title,
          'post_type'=>'my_post_type',
          'posts_per_page'=> 1,
        );
        $the_query = new WP_Query( $args );
        if ( 1 == $the_query->post_count ) {
          $content .= "[custom-trick id={$the_query->posts[0]->ID}]";
        }
        $my_post = array(
              'ID'           => $ID,
              'post_content' => $content,
        );
        update_post_meta( $ID, "first_time_published", true );
        wp_update_post( $my_post );
    
    }
    add_action( 'publish_custom-trick', 'update_post_content', 10, 2 );

    Can you see how it works ordered thusly? Note that the second assignment to $content has a dot in the operator: .=, appending what is to the right to whatever is already assigned to $content, namely the initial shortcode.

    Thread Starter johnkelton

    (@johnkelton)

    lets see, I am trying now, stay tuned!

    Thread Starter johnkelton

    (@johnkelton)

    Ok Problemo:

    It doing everything right, except, it had the same ID in both shortcodes , this id is of the newly created and published post

    First shortcode whould have the ID of Matched Post and Second Shortcode should have the ID of this new published post

    [custom-trick id=342][custom-trick id=342]

    It should be

    [custom-trick id=234][custom-trick id=342]

    • This reply was modified 7 years, 8 months ago by johnkelton.
    Thread Starter johnkelton

    (@johnkelton)

    when I did this:

          'posts_per_page'=> 2, //Added 2 instead of 1
        );
        $the_query = new WP_Query( $args );
        if ( 2 == $the_query->post_count ) { // two identical posts
          $content .= "[custom-trick id={$the_query->posts[1]->ID}]";

    //index = 1

    this seems to be working, what you think?

    • This reply was modified 7 years, 8 months ago by johnkelton.
    Thread Starter johnkelton

    (@johnkelton)

    Thanks a lot for your help!

    Moderator bcworkz

    (@bcworkz)

    You’re welcome!
    I’m not sure why your changes are working, but if it works, don’t fix it!
    Clearly my version was finding the new version before the older, I was expecting the other way around. Given the reversal, your way is fine.

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘Finding Posts with Same title and then Edit them’ is closed to new replies.