Snippets Collections
add_filter('post_type_link', 'custom_post_type_link', 10, 3);
function custom_post_type_link($permalink, $post, $leavename) {
    if (!($post instanceof WP_Post)) {
        return $permalink;
    }

    if ($post->post_type === 'movies') {
        return get_home_url() . '/' . $post->post_name . '/';
    }

    return $permalink;
}

add_action('pre_get_posts', 'custom_pre_get_posts');
function custom_pre_get_posts($query) {
    global $wpdb;

    if (!$query->is_main_query()) {
        return;
    }

    // Get the requested post name
    $post_name = $query->get('name'); // Use 'name' instead of 'pagename'

    if (empty($post_name)) {
        return;
    }

    // Fetch post type from database
    $result = $wpdb->get_row(
        $wpdb->prepare(
            "SELECT wpp1.post_type, wpp2.post_name AS parent_post_name 
             FROM {$wpdb->posts} AS wpp1 
             LEFT JOIN {$wpdb->posts} AS wpp2 ON wpp1.post_parent = wpp2.ID 
             WHERE wpp1.post_name = %s LIMIT 1",
            $post_name
        )
    );

    if (!$result) {
        return; // Ensure we have a valid result
    }

    $post_type = $result->post_type; // Fix: Define $post_type

    if ($post_type === 'movies') {
        $query->set('post_type', $post_type);
        $query->set('name', $post_name);
        $query->is_single = true;
        $query->is_page = false;
    }
}
// models.py
class Product(models.Model):
    title = models.CharField(max_length=225, unique=True)
    slug = models.SlugField(max_length=225, unique=True, null=True)

// admin.py
class ProductAdmin(admin.ModelAdmin):
    prepopulated_fields = {'slug': ('title',)}

// views.py
class productDetail(generic.DetailView):
    model = Detail
    template_name = 'detail.html'

// urls.py
    path('product/<slug:slug>/', views.productDetail.as_view(), name='productDetail'),
// create a schema
const eventSchema = new Schema({
  name: String,
  slug: {
    type: String,
    unique: true
  },
  description: String
});

// create the model
const eventModel = mongoose.model('Event', eventSchema);

// middleware -----
// make sure that the slug is created from the name
eventSchema.pre('save', function(next) {
  this.slug = slugify(this.name);
  next();
});

// function to slugify a name
function slugify(text) {
  return text.toString().toLowerCase()
    .replace(/\s+/g, '-')           // Replace spaces with -
    .replace(/[^\w\-]+/g, '')       // Remove all non-word chars
    .replace(/\-\-+/g, '-')         // Replace multiple - with single -
    .replace(/^-+/, '')             // Trim - from start of text
    .replace(/-+$/, '');            // Trim - from end of text
}
star

Mon Oct 11 2021 04:11:32 GMT+0000 (Coordinated Universal Time)

#django #ecommerce #slug
star

Mon Jun 14 2021 12:21:17 GMT+0000 (Coordinated Universal Time) https://scotch.io/courses/create-a-crud-app-with-node-and-mongodb/a-mongoose-model

#express #nodej #mongodb #mongoose #slug #url

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension