Jump to content

Search by the number of characters in the title of the page with selector


Mika
 Share

Recommended Posts

Hello, can I use the selector to extract only pages with page titles longer than 10 characters?

I'm looking for a way to search directly without using the php foreach statement, as shown below.

        $result = $this->pages->find("template!=admin, has_parent!=2, include=all");
        foreach ($result as $page) {
            if (strlen($page->title) > 10) {
                $this->table[] = $page;
            }
        }
 

Link to comment
Share on other sites

Welcome to the PW forums ?

PW doesn't currently provide a way to find pages by the length of a field value. Although I think it would be a nice feature so I took the liberty of opening a request: https://github.com/processwire/processwire-requests/issues/424

Technically you could achieve it with a custom SQL query:

$sql = 'SELECT pages.id FROM pages LEFT JOIN field_title ON field_title.pages_id = pages.id WHERE CHAR_LENGTH(field_title.data) > 10 AND pages.templates_id != 2 AND pages.parent_id != 2 AND pages.parent_id NOT IN (SELECT pages_id FROM pages_parents WHERE parents_id = 2 OR pages_id = 2)';
$ids = $database->query($sql)->fetchAll(\PDO::FETCH_COLUMN);
$results = $pages->getByIDs($ids);

But you might find it simpler to stick with what you're already doing.

  • Like 3
Link to comment
Share on other sites

Hi,

as Robin said, so far it's not a native pw selector and, being a great fan of sql queries i do like his solution ?

now if you want to take advantage of what pw gives you with selectors, you can just add a numeric field to the templates you want to find
name it as you want (i'll use titlelength for my example)

in the field tab of the... field set it as hidden

then in your ready.php file put something like this (the namespace is just here in case you don't have a ready.php file for the moment)

<?php namespace ProcessWire;

if(!defined("PROCESSWIRE")) die();

$pages->addHookAfter('saveReady', function($e) {
    $p = $e->arguments(0);
    if($p->template != 'template_to_play_with') return; // you could also use an array of templates to exclude and if (in_array($p->template, $your_array))
    $ln = strlen($p->title);
    $p->set('titlelength', $ln);
});

now you can filter using titlelength>10

hope it helps

have a nice day

  • Like 2
Link to comment
Share on other sites

On 11/16/2021 at 11:56 AM, Robin S said:

Welcome to the PW forums ?

PW doesn't currently provide a way to find pages by the length of a field value. Although I think it would be a nice feature so I took the liberty of opening a request: https://github.com/processwire/processwire-requests/issues/424

Technically you could achieve it with a custom SQL query:

$sql = 'SELECT pages.id FROM pages LEFT JOIN field_title ON field_title.pages_id = pages.id WHERE CHAR_LENGTH(field_title.data) > 10 AND pages.templates_id != 2 AND pages.parent_id != 2 AND pages.parent_id NOT IN (SELECT pages_id FROM pages_parents WHERE parents_id = 2 OR pages_id = 2)';
$ids = $database->query($sql)->fetchAll(\PDO::FETCH_COLUMN);
$results = $pages->getByIDs($ids);

But you might find it simpler to stick with what you're already doing.

Another cool way to do this would be to let ProcessWire take care of the main selector string, because you don’t really want to write page status checks and all that stuff yourself.

$query = $pages->getPageFinder()->find('template!=admin, has_parent!=2, include=all', [ 'returnQuery' => true ]);

$statement = $query->leftjoin('field_title on field_title.pages_id = pages.id')
                   ->where('char_length(field_title.data) > 10')
                   ->prepare();

$database->execute($statement);
$ids = $statement->fetchAll(\PDO::FETCH_COLUMN);
$results = $pages->getByIDs($ids);

For this purpose you could also do the join and the condition in one call using join().

  • Like 2
Link to comment
Share on other sites

4 hours ago, DV-JF said:

@Mika may I ask you, out of interest, what the specific use case is for you?  Why would you want to search for a title that is at least longer than 10 characters? ? Cheers!

I'm developing a module for SEO. This is because it is said that 50 to 60 characters is a good for the length of the page title.
The 10 characters are only for my test environment.?

Link to comment
Share on other sites

@Mika Hi, don't be confused by this char length thing about the page title, it actually concerns the page title tag not the title of the page
the title tag is not a native pw field and and would not advise you to use the page title for both things

as far as SEO is concerned, google indeed said that the first 60/65 chars are really useful (taking into account that the more a word is at the beginning the stronger it is
in a title tag, you don't make a keywords list but you are not too literary either and you wouldn't like your displayed page title to look like this tag ?

have a nice day

Link to comment
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
 Share

×
×
  • Create New...