Support » Fixing WordPress » Filtering post_results

  • Lee Willis


    I’m trying to apply a filter to post_results so that I can search a WordPress application’s custom fields and return matching application objects (Think products in a store, or branches in a store directory). The inidividual objects don’t exist as specific posts or pages – rather there is a single page who’s output is filtered to include the application output.

    I’ve managed to get a filter function running to identify the permalinks that I need to return (E.g. /shop/product1/ or /directory/branch1) and I can identify the page ID that the application normally resides at – but I can’t work out a way of passing this through to be displayed. If I add the page to the results array then just the basic page gets displayed – not the specific product. Is there a way to pass the permalink in so that the right content gets displayed – or am I going about this totally wrong??

Viewing 5 replies - 1 through 5 (of 5 total)
  • stvwlf


    If you mean to pass query_vars as part of a URL there are two approaches I know of.


    A) Add to functions.php

    add_filter('query_vars', 'lg_queryvars' ); //  query vars passed on URL
    function lg_queryvars( $qvars ) {
      $qvars[] = 'productID';
      return $qvars;

    b) pass the parameter on the URL in the link in the program where you need it
    <a href="/shop/?product_id=$variable_name">click here</a>

    c) in the shop page retrieve the query var value
    $productID = $wp_query->query_vars['productID'];

    Lee Willis


    Erm, sort of. Query vars are already being used (Although they’re mapped to a permalink structure).

    The problem is that I have one page (ID 19) that is used for the whole of my app – but I want SEARCHES to be able to return links direct into my app (E.g. to shop a specific product in an ecommerce app).

    I can extend the list of matching search results by adding a filter to posts_results that searches my app’s tables and works out which products match – but I can’t find a way of specifying the permalink URL so that it knows that the match is PAGE ID 19 WITH A SPECIFIC SET OF QUERY VARS, not just PAGE ID 19.

    I’m actually thinking it’s not possible – but that would be inconvenient to say the least 🙂



    You have to modify the search template to add the query vars onto the end of the link it presents. That is what these two methods are for.

    What happens is WP discards qvars it doesn’t know of from URL’s. You have to register your qvar with WP so to speak. That is what the two methods are for.

    The first method lets you access them as $_GET in the receiving program, the second adds them to WordPress internal qvar structure so they are available for use in the receiving program.

    The method in the first article allows you to retrieve the qvar in the receiving program as a _GET variable. I’m not using it in a search application but I use it to pass profile ID’s to a single static page template. I have several thousand profiles in a custom db table and I display all of them on one static page by adding the profile ID as a qvar added on the URL.

    Lee Willis


    I still think we’re at cross purposes?

    My application already has the right pages available for, e.g.

    These pages work – and show the right content. (Essentially they’re both page id 19, but with a query arg) The problem comes because I’ve extended the site search by adding a filter to posts_results. This queries against my product description to find matching products, then tries to add these to the results array. All I can do though is add page id 19 to the array – there’s no way to add the fact that the matching page is (Page id 19 + query var with value “product1”) – or to add the correct permalink (Which I know at this point in my code).

    The end result is that when the array is consumed by the theme code (Which calls the_post, the_permalink etc.) it spits out the default content for page 19 – not the individual products.

    So the question isn’t about how to make query_args work for building an application – but how to pass them up the chain of posts_results when performing a search – make sense?

    Lee Willis


    No-one else got any other ideas? Does wordpress need extending to allow it to store / parse query_vars when passing the search results array? Should I look into that – would it be accepted as a patch?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Filtering post_results’ is closed to new replies.