AmphiBee / wordpress-eloquent-models

ORM Eloquent pour WordPress
4 stars 3 forks source link

Query posts by language with polylang #8

Open ligne13 opened 2 months ago

ligne13 commented 2 months ago

Hi,

I'm using Polylang: is there a way to query posts by language?

Thank you.

tgeorgel commented 2 months ago

Hi @ligne13

You can use https://gitlab.com/tgeorgel/object-press

composer require tgeorgel/objectpress

It supports Polylang natively and uses this package underneath :

<?php

namespace App\Models;

use OP\Framework\Models\Concerns\PolylangTranslatable;
use OP\Framework\Models\Post as BasePost;

class Post extends BasePost
{
    use PolylangTranslatable;
}

/**
 * Usage : query builder
 */

// current language
$posts = Post::published()->language('current')->get();

// default language
$posts = Post::published()->language('default')->get();

// specific language
$posts =  Post::language('en')->get();

// post that has any language set up
$posts =  Post::hasLanguage()->get();

/**
 * Usage : model instance
 */

$post->language; // get language for this model, eg: 'fr'
$post->language = 'en'; // set 'en' as the post language

$post->translation('en') // get the 'en' translation of this model
$post->translation('current') // get the current language translation of this model

Read the doc to see other features : https://object-press.wp-grogu.dev/#/