Leftfield

[SOLVED] Jump x Checkfields

Recommended Posts

$cards->prepend($pages->find("template=blog-post, blog_post_fav='', blog_post_favs='', blog_post_sticky!=1, sort=-blog_date, limit=" . $blogSettings->blog_quantity));

I am stuck with my nervs. After like 300 lines I am done.

Trying to exclude only first blog_post_fav and first two blog_post_favs.

Share this post


Link to post
Share on other sites

I am tying my best to understand what you are tying to achieve, but still do not understand what you're after. Please explain yourself better. The only thing I can suggest now is maybe not doing it all in one selector...

<?php

$cards->prepend($pages->find("selector1"));
$cards->prepend($pages->find("selector2"));

// or a one-liner with a fluent interface
$cards->prepend($pages->find("selector1"))->prepend($pages->find("selector2"));

 

Share this post


Link to post
Share on other sites

Thank You for helping me Ivan!

I got 3 checkboxes in every article.

I am trying to exclude articles, sorted by date, which had checkbox checked:
Display all blog_post_sticky but
blog_post_fav exclude last article
blog_post_favs exclude last two articles.

blog_post_fav>1
blog_post_favs>2

Share this post


Link to post
Share on other sites

Ok, I probably messed up a few times, but to give an idea of how it could be done (not tested, maybe not the fastest nor the most beautiful code):

<?php

// Get all the blog posts with blog_post_fav and blog_post_favs unchecked
$pageArrayToPrepend = $pages->find("template=blog-post, blog_post_fav=0, blog_post_favs=0, blog_post_sticky!=1");

// Add all posts with blog_post_fav checked without the 1st one
$pageArrayToPrepend->add($pages->find("template=blog-post, blog_post_fav=1, blog_post_sticky!=1, sort=-blog_date")->slice(1));

// Add all posts with blog_post_favs checked without the 1st two items
$pageArrayToPrepend->add($pages->find("template=blog-post, blog_post_favs=1, blog_post_sticky!=1, sort=-blog_date")->slice(2));

// Remove duplicates
$pageArrayToPrepend = $pageArrayToPrepend->unique();

// Sort and limit
$pageArrayToPrepend->filter("sort=-blog_date, limit=" . $blogSettings->blog_quantity));

// Prepend $cards (is there anything in $cards, maybe this is useless variable?)
$cards->prepend($pageArrayToPrepend);


$pages->find returns a PageArray which inherits WireArray with a bunch of useful properties. Read here https://processwire.com/api/ref/wire-array/

 

  • Thanks 1

Share this post


Link to post
Share on other sites

@Ivan Gretsky thank you for helping!!! I really appreciate!!! 

Here's how I solved thanks to my friend @OLSA:

$exclude = new PageArray();
$exclude->add($fav);
$exclude->add($favs);

$cards = $pages->find("template=blog-post, id!=$exclude, blog_post_sticky!=1, sort=-blog_date, limit={$blogSettings->blog_quantity}");

 

  • Like 2

Share this post


Link to post
Share on other sites

@Leftfield I am happy that you solved your problem ;).

Maybe you can consider and few other options to get granularity or selection inside different types of featured posts (favorites).

Option 1)

Using Autocomplete page reference field
Create and place Page Autocomplete field on some template (eg. on home or on some "widget" template).

featured.thumb.png.51b0830e408e1a39d9a0eeb836f71202.png

As example, if field "featured" is on Home page:
 

$fav = $homepage->featured->eq(0); // or $homepage->featured->first
$favs = $homepage->featured->slice(1); // skip first and get others
$cards = $pages->find("template=blog-post, id!={$homepage->featured}");


Option 2)

Get pages by priority levels

Currently you are going with 3 check fields to get "sticky", "fav", and "favs".
There is option how to get the same (checkboxes/radio) with only 1 field, but here I will try to describe (something different) how you can get "granularity" inside featured posts also using 1 field.

Create integer field, and as example, set min to 0, and max to 3, and place it on your "blog-post" template.

priority.png.4d44c65fa1aa1f065ad31702d75c797e.png

Later, in your code, it's very easy to get desired post by priority level.

$fav = $pages->findOne('template=blog-post, priority=3, sort=-blog_date');
$favs = $pages->find('template=blog-post, priority=2, limit=2, sort=-blog_date');
// etc...
$all_others = $pages->find('template=blog-post, priority<1, sort=-blog_date');

With this you can "describe the importance" of some post, and later, if it's needed,  you can extend that with more levels without additional fields (changing only integer field range). Also with this option you can order posts by priority (sort=-priority).

Option 3)

Post priority levels with the addition of hooks (need to test)
Here is option (for testing) to get on the website only 1 post with priority level 3, and later when someone add new page with the same level (3), previously post automatically would go to level 2, and so on. 

Here is working example using hooks, but please note that in some situation maybe could not works as expected.

Before using this hook, you need to create "priority" field (option 2) and set your template inside hook part.
Place this hook inside site/ready.php.

// site/ready.php
/* 
Before using this hook please create integer field "priority"
and place it on desired template.
 */
$this->addHookAfter('Pages::saveReady', function(HookEvent $event){

	$page = $event->arguments[0];

    // if not desired template do nothing
	// note: set your own template
	if($page->template != 'blog-post')return;

    // priority level limit	
	$limit = array(
        1 => 3, // priority level 1, allowed max. 3 pages
        2 => 2, // priority level 2, allowed max. 2 pages
        3 => 1  // priority level 3, allowed max. 1 page
    );

    // processing only if priority has changed and not changed to zero
    if($page->isChanged('priority') && $page->priority > 0) {

		$level = 3; // max priority level

		while($level > 0) {

            // test with different "sort" selector (eg. -modified, -created, or by custom date field)
            $group = $this->pages->find("template=blog-post, priority=$level, sort=-modified");

			if($level == $page->priority) {
                $count = $group->has($page) ? $group->count : $group->count + 1;
            } else {
                $count = $group->has($page) ? $group->count - 1 : $group->count;
            }

			if($count >= $limit[$level]) {
                for($i = 1; $i <= ($count - $limit[$level]); $i++) {
                    $group->eq($group->count - $i)->setAndSave('priority', $level - 1);					
                }
            }
            $level = $level - 1;
        }
    }
});

Regards.

  • Like 2
  • Thanks 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.