kayue / KayueWordpressBundle

A Symfony 2 bundle for providing WordPress repositories and authenticating users (login).
101 stars 43 forks source link

Get all post by it's term slug #28

Closed dqnguyen1989 closed 10 years ago

dqnguyen1989 commented 10 years ago

Hi,

Can I get all post by desire term slug( eg:uncategorized ) ? I'm using Wordpress as backend and Symfony2 to get all data.

Thanks

kayue commented 10 years ago

@dqnguyen1989 Absolutely, and I use QueryBuilder for this

        $qb = $this->getEntityManager()->createQueryBuilder()
            ->from('KayueWordpressBundle:post', 'post')
            ->select('post, tax, term')
            ->leftJoin('post.taxonomies', 'tax')
            ->leftJoin('tax.term', 'term')
            ->andWhere('tax.name = :tax')
            ->andWhere('term.slug = :term')
            ->andWhere('post.type = \'post\'')
            ->andWhere('post.status = \'publish\'')
            ->setParameter('tax', $tax)
            ->setParameter('term', $slug);
dqnguyen1989 commented 10 years ago

@kayue : It excuted FatalErrorException Call to undefined method Bundle\Controller\DefaultController::getEntityManager().

I used namespace: use Kayue\WordpressBundle\Doctrine\WordpressEntityManager; use Kayue\WordpressBundle\Entity;

Thanks for helping.

kayue commented 10 years ago

Oops it is a private method of mind. Try to use $this->get('kayue_wordpress.post.manager') (In fact Doctrine's default entity manager doctrine.orm.default_entity_manager should work as well if you are in the same database and things are configured properly.)

dqnguyen1989 commented 10 years ago

Sorry but where can I find document for this bundle, I'm confused. And how to used functions listed on your README. Your bundle is great but I can't find document.

kayue commented 10 years ago

@dqnguyen1989 The read me should have everything. Which part is blocking you?

As long as your Symfony app is in the same database as the WordPress, all the entity should just work.

(WordPress Authentication is tricky but it should work too..)

dqnguyen1989 commented 10 years ago

OK, I'm cleared, but How can I getContent like apply_filters('the_content') on wordpress, i'm using php, not twig

kayue commented 10 years ago

The usage is exactly the same as normal entity $post->getContent(), this will return the raw value from database.

To get format like normal WordPress (with p tags etc), you will need to use the wp_autop filter.

https://github.com/kayue/KayueWordpressBundle#usage

dqnguyen1989 commented 10 years ago

wp_autop is twig function ? I'm using php for this.

kayue commented 10 years ago

@dqnguyen1989 Although it is a twig function, it is also a PHP function. You will need a way to access the WordpressExtension class. (or copy and paste this function to your app.)

https://github.com/kayue/KayueWordpressBundle/blob/master/Twig/Extension/WordpressExtension.php

dqnguyen1989 commented 10 years ago

I'm thank you your helping but I receive null data when I run $qb = $this->get('doctrine.orm.default_entity_manager')->createQueryBuilder() ->from('KayueWordpressBundle:post', 'post') ->select('post, tax, term') ->leftJoin('post.taxonomies', 'tax') ->leftJoin('tax.term', 'term') ->andWhere('tax.name = :tax') ->andWhere('term.slug = :term') ->andWhere('post.type = \'post\'') ->andWhere('post.status = \'publish\'') ->setParameter('tax', $tax) ->setParameter('term', $slug);

kayue commented 10 years ago

That means no posts were found. Maybe try to remove all the where statements?

$qb = $this->get('doctrine.orm.default_entity_manager')->createQueryBuilder()
->from('KayueWordpressBundle:post', 'post')
->select('post')
->getQuery()->getResult()
dqnguyen1989 commented 10 years ago

@kayue : Thanks for all helping. It's run with this code

$posts = $this->get('doctrine.orm.default_entity_manager')->createQueryBuilder() ->from('KayueWordpressBundle:post', 'post') ->select('post') ->leftJoin('post.taxonomies', 'tax') ->leftJoin('tax.term', 'term') ->andWhere('tax.name = :tax') ->andWhere('term.slug = :term') ->andWhere('post.status = \'publish\'') ->setParameter('tax', 'category') ->setParameter('term', $category_slug) ->getQuery()->getResult();

kayue commented 10 years ago

@dqnguyen1989 Can you try the following see if it is returning any post? Just want to make sure the bundle is working.

$this->getDoctrine()->getRepository('KayueWordpressBundle:Post')->findOneById(1);

// or even return all posts!
$this->getDoctrine()->getRepository('KayueWordpressBundle:Post')->findAll();

If it works, then you can debug your query:

// build query builder...
var_dump($qb->getQuery()->getSql());
exit;
dqnguyen1989 commented 10 years ago

The bundle work correctly. Thanks :+1:

kayue commented 10 years ago

Good to hear that :+1: