Skip to content

Instantly share code, notes, and snippets.

@carbontwelve
Last active January 8, 2016 14:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carbontwelve/6311497 to your computer and use it in GitHub Desktop.
Save carbontwelve/6311497 to your computer and use it in GitHub Desktop.
Laravel model for wordpress wp_term table
<?php
/* Import.php */
namespace App/Controllers;
use /App/Models/TermTaxonomy;
use /App/Models/Term;
class Import extends BaseController
{
public function index()
{
/* Get all categories */
$terms = TermTaxonomy::with(
array(
'parent',
'parent.term'
'term'
)
)
->where('parent', 0)
->where('taxonomy','category')
->get();
}
}
<?php
/* Term.php */
namespace App/Models;
class Term extends Eloquent
{
protected $table = 'wp_terms';
protected $primaryKey = 'term_id';
public function taxonomy()
{
return $this->hasOne('/App/Models/TermTaxonomy', 'term_id');
}
}
<?php
/* TermTaxonomy.php */
namespace App/Models;
class TermTaxonomy extends Eloquent
{
protected $table = 'wp_term_taxonomy';
protected $primaryKey = 'term_taxonomy_id';
public function term()
{
return $this->belongsTo('/App/Models/Term', 'term_id');
}
public function children()
{
return $this->hasMany('/App/Models/TermTaxonomy', 'parent');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment