Generate slug and meta data if meta field is empty

Ok, after a little playtime, here is the answer: add_action(‘save_post’, ‘ocp_jobs_save_details’); function ocp_jobs_save_details(){ global $post; $genref = sanitize_title( dechex( time() ) ); if ( $post->post_type == ‘job’ ) { if ( $_POST[‘ocp_jobs_ref’] ) { $jobs_args = array( ‘ID’ => $post->ID, ‘post_name’ => strtolower( sanitize_title( $_POST[‘ocp_jobs_ref’] ) ) ); $myref = strtoupper( $_POST[‘ocp_jobs_ref’] ); } else{ … Read more

Custom taxonomy page template

You should follow appropriate naming convention (aka Template Hierarchy) of your templates to make your job done. If I’m clear about your query here then I’d suggest you to name your custom taxonomy archive template(s) as “taxonomy-{your-taxonomy-name-here}.php” (i.e. taxonomy-vegetable.php, taxonomy-fruit.php and so on) There must not be any blank space or underscore between two words … Read more

Custom post type clean url

Generate WP can help you generate a clean CPT. If this doesn’t work, try the site to make sure you didn’t miss anything. // Register Custom Post Type function custom_post_type() { $labels = array( ‘name’ => _x( ‘Teams’, ‘Post Type General Name’, ‘text_domain’ ), ‘singular_name’ => _x( ‘Team’, ‘Post Type Singular Name’, ‘text_domain’ ), ‘menu_name’ … Read more

Heirarchical URLs with CPT and custom taxonomy

In both your rewrite arguments, add ‘with_front’ => false, then for your taxonomy change ‘slug’ => ‘topics’ to ‘slug’ => ‘knowledgebase’. Finally, switch the order of your registration – so that register_taxonomy is called first. This will place the taxonomy rewrite rules above the post type rules (otherwise the latter will catch the former, since … Read more

Custom Post Type only display items created by user

Instead of coming up with this functionality myself and re-inventing the wheel I’m using the built-in functionality that makes a post ‘private’. By using this I’ve had to force all posts to be private, I’ve done so using the wp_insert_post_data filter: add_filter(‘wp_insert_post_data’, array($this, ‘post_data_save’), 10, 2); And my method: public function post_data_save($post){ if($post[‘post_type’] == ‘my_custom_post_type’){ … Read more

Test date object against previous date in loop

You need something like this: $args = array( ‘post_type’ => ‘post’ ); $q = new WP_Query($args); if ($q->have_posts()) { $date=””; while ($q->have_posts()) { $q->the_post(); $m = get_post_meta($post->ID,’field_id’,true); // var_dump($m); if ($date != $m) { // if $m is a strtotime compatible string echo date(‘l’,strtotime($m)); $date = $m; } echo ‘<br>’; the_title(); echo ‘<br>’; echo str_repeat(‘-‘,200); … Read more