• Resolved antonskitter

    (@antonskitter)


    I have created a custom post type with Pods. Now I want to generate the page title based on Pods values of the post. I created a small function for this:

    function set_car_title( $data , $postarr ) {
      if($data['post_type'] == 'auto') {
    	  
      	$pod = pods( 'auto', get_the_id() );
      	$merk = $pod->display('automerk');
      	$model = $pod->display('automodel');
        $data['post_title'] = $merk." ".$model;
        $data['post_name'] = $merk." ".$model;
      }
      return $data;
    }
    add_filter( 'wp_insert_post_data' , 'set_car_title' , '10', 2 );

    This works fine when I update the custom post but not when creating a new one. So if I create a new custom post it does not work but when i update it the title gets set.

    I think it’s because the pods function runs before there is an id assigned. Anyone any ideas on how to fix this?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Jim True

    (@jimtrue)

    You should be using the pods_api_post_save_pod_item_{podname} filter instead.

    Here’s the documentation link: https://pods.io/docs/code/action-reference/pods_api_post_save_pod_item_podname/

    • This reply was modified 6 years, 6 months ago by Jim True.
    Thread Starter antonskitter

    (@antonskitter)

    Thanks for the fast support. So I should have 2 functions? First pods_api_post_save_pod_item_{podname} and then the one I used in the original post?

    Plugin Contributor Jim True

    (@jimtrue)

    nope, you would use pods_api_post_save_pod_item_auto in place of wp_insert_post_data in your add_filter above.

    Thread Starter antonskitter

    (@antonskitter)

    I have tried it but cannot get it to work. How to get the values from the page to pass in the function? Like model, brand, etc. In my case i have a relation with a custom taxonomy “brands” which has an id to the brand as value. How to get the brand name passed in the title?

    Plugin Contributor Jim True

    (@jimtrue)

    You would need to locate those fields within the $pieces array like the examples found on both of those pages. If you’re not comfortable with the PHP, you are welcome to drop onto our Slack Chat at https://pods.io/chat/ and see if you can hire a developer to help you with it, but it’s pretty much required for you to have a good handle on PHP if you’re going to be working with action hooks and filters. Without knowing your exact structure, all your fields, how they connect and how they’re named, there’s no way we can provide you the answer easily.

    If you’re working with relationship values and those are the ones you want to replace the post_title, you’re going to have to open up connections to the other post types and grab those values as part of your post-save filter. Those values aren’t ‘in the page’ they’re displayed in the form, but they aren’t part of the $pieces array, because the only connection is the post ID of the post on the other side of the relationship. You could fairly easily grab that post id for ‘brands’ from your $pieces value and

    
    $brands = pods('brand',$brand-id);
    $brand-name = $brands->display('post_title');
    

    I’m not a PHP Developer, but that’s pretty much what you’re doing if you’re trying to pull values from your other relationship fields and write them into the post-title of this post you’re saving: you’re opening those pods objects and looking up the record by it’s id and pulling it’s ‘name’ out of the post_title.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Custom page title on saving post’ is closed to new replies.