Wordpress - Fetching posts / pages


When to use pages, when to use posts?

There are two types of article contents in WordPress: page and post. The type post is only used where there is a date-based list of fresh content, such as news articles or blog posts.

For all other fixed content you should use the page type. You can use page hierarchy to create categories of pages and lists of pages. You can also add pages to menus (this is not possible for posts). See below for docs.

Fetching a list of posts/pages

To fetch a list of all posts/pages you can simply use WpPost::fetch() or WpPage::fetch() in your controller and then use foreach to run through them. You can also paginate as you would regular fetcher objects.

Here's a short example:

Page hierarchy

Page hierarchy should be used to list subpages of a parent page. You can get the subpages of a page using the post_parent, post_parent__in, or post_parent__not_in WpQuery parameters (see docs).

If you want to get the parent of a specific page use {{wpppost.parent}} (not .data.parent!). For example the id of the title of the parent page is {{wppost.parent.title}}. See WP connections and data for more info.

Getting a post/page using Wp_Query

As defined in the WpQuery documentation, you need to change the query string a bit to fetch pages.

For pages you should use WpPost::fetch() and for posts you should use WpPost::fetch(). This will return only the type of WordPress content you request.

For post slugs, use:

$this->zajlib->variable->wppost = WpPost::fetch()->query('name=my-post-slug')->next();

For page slugs, use:

$this->zajlib->variable->wppost = WpPage::fetch()->query('pagename=my-page-slug')->next();

With both posts and pages, slugs should work properly with WPML (see below).

Advanced Wp_Query

You can use any of the Wp_Query parameters to fetch posts or pages. See WordPress documentation on fetching posts by WpQuery for more info.

To use the Wp_Query parameters with Outlast Framework's object model, you have two options. You can pass the query as string:

$drafts = WpPost::fetch()->query('post_status=draft');

Or you can use the array syntax:

$drafts = WpPost::fetch()->query(array(
	'post_type' => array( 'post', 'page', 'movie', 'book' )
));

Finally, there is also a filter() syntax which provides a more Outlast Framework compatible syntax:

$drafts = WpPost::fetch()->filter('post_status', 'draft');

All three methods result in the same Wp_Query. Refer to Wp_Query documentation for a full list of available options.

Getting a page/post by id

Fetch post 12:

$post = WpPost::fetch(12);

Fetch page 34:

$page = WpPage::fetch(34);

You should typically use WpQuery slug instead, because that method is compatible with multi-lang.

Getting children of a page using WpQuery

In most cases you can simply use the children field within the WpPage object:

{% foreach wppage.children as subpage %}

You can also use WpQuery - here's an example of using page id to get the page's children:

$this->zajlib->variable->pages = WpPage::fetch()->query('post_parent=11');

...you can also use the array() notation:

$this->zajlib->variable->pages = WpPage::fetch()->query(array('post_parent'=>11));

...or the filter() notation:

$this->zajlib->variable->pages = WpPage::fetch()->filter('post_parent', 11);

Note: Using WpPage::fetch() is exactly the same as using WpPost::fetch()->filter('post_type', 'page'). It is however more semantic and shorter. So use WpPage if it is a page.

Multilingual posts using WPML

WPML will handle posts correctly, so that if you get the post by a slug in one language then it will return the post in the current language.

So if the language is set to hu_HU, then the following:

$this->zajlib->lang->set('hu_HU');
$this->zajlib->variable->wppost = WpPost::fetch()->query('name=my-english-slug')->next();

...will actually return the Hungarian version of the post with the slug my-english-slug.

Fetching posts by Simple Field value

Check out Custom posts and fields for info on Simple Fields.

Outlast Web & Mobile Development (c) 2023 | Privacy Policy |