You are here:  » Short Code - Limit Results


Short Code - Limit Results

Submitted by stew on Thu, 2015-08-06 07:12 in

Hi David,

Hope all's well.

Is there a way to limit the number of results produced by the shortcode with the latest plugin version?

For example:

[pto search="jumper" results="20"]

or similar?

(I had a look through the forum and found a similar query but I think the code has changed since that solution)

Will see what you think,

Many Thanks,

Stew

Submitted by support on Thu, 2015-08-06 07:49

Hello Stew,

Sure - in pto.php look for the following code at line 400:

  if (isset($atts["maxprice"])) $pto_maxPrice = $atts["maxprice"];

...and REPLACE with:

  if (isset($atts["maxprice"])) $pto_maxPrice = $atts["maxprice"];
  global $pto_results;
  if (isset($atts["results"])) $pto_results = $atts["results"];

And then in pto_search.php look for the following code at line 328:

  $sql .= " LIMIT ".$offset.",".$pto_config_resultsPerPage;

...and REPLACE with:

  global $pto_results;
  if (isset($pto_results))
  {
    $sql .= " LIMIT ".$pto_results;
  }
  else
  {
    $sql .= " LIMIT ".$offset.",".$pto_config_resultsPerPage;
  }

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by stew on Thu, 2015-08-06 08:18

H David,

Brilliant! That worked great,

Many thanks for that,

Stew

Submitted by nanaz on Mon, 2016-02-08 13:28

Hi David,

I already changed pto_featured as decribed on http://www.pricetapestry.org/node/33 to limit products by using shortcode, works great.

As to limiting the searchresults with the script above; is it also possible to display products randomly and hide the pagenavigation (only when the results limit is set in the shortcode)? Would be a great way to display categoryproducts in articles.

Hope you can help!

Cheers, Marlies

Submitted by support on Mon, 2016-02-08 13:55

Hi Marlies,

You can use the same technique as described above to pass through a sort order parameter in the shortcode. In pto.php look for the following code at line 400:

  if (isset($atts["maxprice"])) $pto_maxPrice = $atts["maxprice"];

...and REPLACE with:

  if (isset($atts["maxprice"])) $pto_maxPrice = $atts["maxprice"];
  global $pto_sort;
  if (isset($atts["sort"])) $pto_sort = $atts["sort"];

Then in pto_search.php look for the following code at line 308:

  $orderBy["rating"] = "rating DESC";

...and REPLACE with:

  $orderBy["random"] = "RAND()";

Then for 20 random results for category "Widgets", you could use:

[pto search="category:Widgets" results="20" sort="random"]

"out of the box", page navigation links are not be displayed when using the search shortcode, but there are mods on the forum to change this so if you have already applied such changes let me know and I'll work though how to disable conditionally for you...

Cheers,
David.
--
PriceTapestry.com

Submitted by nanaz on Mon, 2016-02-08 14:58

Hi David,

I'm sorry, I now see I'm not being clear enough. I only mentioned the pto_featured code in node 33 as an example for what I would want.

After changing the code as you told Stew on this page in 2015 the searchresult is limited and automatically includes pagenavigation. That's good!

Wat would be ideal is to have the option to turn the pagenavigation off, only for these limited search results. Assume I'm writing an article about roses. I don't have a category 'Roses', since they are all categorized as 'Flowers'. If I can put in [pto search="roses" results="4" pagenav="off"] or something like that, it would save me a lot of work instead of having to work with a featured products list that changes all the time, with every updated feed. And if it could be randomly displayed, that would be even better.

Maybe less complicated would be: IF results in shortcode are set, THEN pagenavigation = off, but you know best :-)

I hope you can help (again)... Thank you!

Marlies

Submitted by support on Mon, 2016-02-08 15:28

Hello Marlies,

No problem - in pto_search.php look for the following code around line 398:

  $html .= pto_search_navigation();

...and REPLACE with:

  if (!isset($pto_results))
  {
    $html .= pto_search_navigation();
  }

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by nanaz on Mon, 2016-02-08 17:02

Hi David,

After some problems with an (old) modification in pto.php it's working now. Happy now, thanks again!

Cheers, Marlies

Submitted by nanaz on Thu, 2017-10-19 08:42

Hi David,

As I am working on another site, now with PTO 3.0, I implemented all the changes above to pto_search.php and pto.php so now it's possible to show random & limited items again. Happy!

{link saved}

I added pagination to the shortcode as described in https://www.pricetapestry.org/node/416 and changed the code in pto.php:

$html .= pto_search();

to

$html .= pto_search();
$html .= pto_search_navigation();

But I'm having a problem with the hiding of the pagination, it's shown on pages with less than 18 products (my $config_resultsPerPage) and also when there are no products available. I changed pto_search.php as described above:

$html .= pto_search_navigation();

into

if (!isset($pto_results)) { $html .= pto_search_navigation(); }

but the pagination is still on and also not working right with the new random search shortcode, with Next pages ending in www.mydomain.com/2, www.mydomain.com/3 etc. I have been working on it last night but can't get it right. I really hope you can help me out again.

If it's possible it would be awesome if pagination could always be ON with search results, except when I use a parameter, something like

[pto search="XXX" results="3" sort="priceDesc" pagination="OFF"]

Thank you so much,

Marlies

Submitted by support on Thu, 2017-10-19 09:11

To make navigation always show (but only when more results than displayed) for normal search and shortcodes by default but
with an attribute to disable navigation via a shortcode attribute, rather than the changes to pto_search.php described
above instead look for the following code at line 419:

    if (
       ($pto_searchResultsTotal > $pto_config_resultsPerPage)
       &&
       pto_common_isContainer()
       )
    {
      $html .= pto_search_navigation();
    }

...and REPLACE with:

    global $pto_searchNoNavigation;
    if (
       ($pto_searchResultsTotal > $pto_config_resultsPerPage)
       &&
       (!isset($pto_searchNoNavigation))
       )
    {
      $html .= pto_search_navigation();
    }

And then in pto.php instead of the modification;

  $html .= pto_search();
  $html .= pto_search_navigation();

...use:

  global $pto_searchNoNavigation;
  if (isset($atts["nonavigation"])) $pto_searchNoNavigation = TRUE;
  $html .= pto_search();

And then add nonavigation="TRUE" to your shortcode to disable navigation. Result count and sort aren't supported by default by I think you have previous mods in place to support those - let me know if you're not sure of course...

Cheers,
David.
--
PriceTapestry.com

Submitted by nanaz on Thu, 2017-10-19 12:22

Hi David,

Yes, I have installed the random and limit searchresults mods as described in this node.

Sorry to say the pagination is still there after applying the new code. A good thing is that now when there are no articles available and the sentence 'No articles found' is displayed, the pagination is gone.

Also the links in pagination are wrong when using the keyword search shortcode [pto search="XXX" results="4" sort="random" nonavigation="TRUE"] with Previous & Next links ending in www.mydomain.com/2, www.mydomain.com/3 etc.

I made some examples here: http://www.leukehorloges.nl/test

Sorry to bother you again and hoping you can help,

Marlies

Submitted by support on Thu, 2017-10-19 12:56

Hello Marlies,

I missed the global declaration of $pto_searchNoNavigation in the changes to pto_search.php - modification corrected above.

In addition to that, in order to generate the correct follow-on pagination links from pages using a shortcode, in pto_search.php look for the following code beginning at line 688:

      if (
         ($pto_sort == "relevance")
         &&
         ($pto_minPrice.$pto_maxPrice.$pto_merchantFilter.$pto_categoryFilter.$pto_brandFilter == "")
         )

...and REPLACE with:

      if (
         (pto_common_isContainer())
         &&
         ($pto_sort == "relevance")
         &&
         ($pto_minPrice.$pto_maxPrice.$pto_merchantFilter.$pto_categoryFilter.$pto_brandFilter == "")
         )

Hope this helps

Cheers,
David.
--
PriceTapestry.com