0

I'm using Wordpress 3.5.1 with a modified twentytwelve theme. My main site structure is based on the Smooth Slider plugin (jQuery Cycle).

I have Pages that contain only a shortcode (for example, [smoothslider id='4']) to display Posts assigned to a slider. When I do a search on the site it returns result for individual posts only (and not for pages, of course). This is not what I want as there is no slider anymore. Those posts are not supposed to be displayed individually.

What I wish to do is instead of having the results with links to the single posts, have result links to the parent page. But posts have no parents so it would need to be taken from the menu_order (?) and set the results to those URLs.


Example

MENU:

  • CAR [Page - Redirect to first child: car-1]
    • car-1 [Page - Content: shortcode with "sliderid=1"]
      • car-1a [Post - attached to sliderid=1 - Content: text+img]
      • car-1b [Post - attached to sliderid=1 - Content: text+img]
      • car-1c [Post - attached to sliderid=1 - Content: text+img]
    • car 2 [Page - Content: shortcode with "sliderid=2"]
      • car-2a [Post - attached to sliderid=2 - Content: text+img]
      • car-2b [Post - attached to sliderid=2 - Content: text+img]

Search results for "car":

  • Results I get:
    • example.com/car-1a,
    • example.com/car-1b,
    • example.com/car-2a
    • etc.
  • Results I want:
    • example.com/CAR/car-1,
    • example.com/CAR/car-2,
    • etc.
3
  • How does Smooth Slider associate those ids with the posts?
    – s_ha_dum
    Commented Apr 24, 2013 at 13:51
  • There's table in the database: "wp_smooth_slider": id, post_id, date, slider_id, slide_order
    – Oh_Franck
    Commented Apr 24, 2013 at 16:21
  • Nobody has any hint?
    – Oh_Franck
    Commented Apr 30, 2013 at 10:20

2 Answers 2

0

Ok I have figured it out; added the following to functions.php:

add_filter( 'posts_request', 'my_request_filter', 10, 2 );

function my_request_filter($sql, $query) {
    if($query->is_main_query() && is_search()) {
        $sql = "SELECT * FROM wp_posts WHERE post_content LIKE '[smoothslider%' AND post_parent !='0'";
    }
    return $sql;
}

I understand that with no access to the database or being able to run test my slider it was only guessing so I'll check your answer as accepted answer anyway. Thank you for your help.

1
  • Oh no! this doesn't work: it returns a complete list of all parents every time. :-(
    – Oh_Franck
    Commented May 1, 2013 at 9:13
0

I don't have your slider installed so I can't test this but something like the following is what you need:

function get_parent_page() {
  if (is_singular()) {
    global $wpdb,$post;
    if (isset($post->ID)) {
      $slide = $wpdb->get_var("SELECT DISTINCT slide_id FROM {$wpdb->prefix}smooth_slider WHERE post_id = {$post->ID} LIMIT 1");
      if (!empty($parent)) {
        $parent = $wpdb->get_var("SELECT ID FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type = 'post' AND post_content LIKE \"%[smoothslider id='".$slide."']%\"");
        if (!empty($parent)) {
           wp_safe_redirect(get_permalink($parent));
        }
      }
    }
  }
}
add_action('template_redirect','get_parent_page');

That is very query intensive, and a LIKE query is not especially efficient, but since you don't have any other way to connect the posts that is the best idea I have.

That should, if it works (again, I can't test it), redirect requests for single page results back to the parent. I recommend this approach because altering the main search query to do this will be (wildly, I think) difficult and almost certainly extremely slow. You would have to write a couple of JOINs and alter the post FIELDs and the WHERE clause. There is no way I could get that to work in any reasonable amount of time.

The other option is to alter your search.php file (or create one) so that the permalinks used point to the parent post. You could use almost the same function just pass in the post ID.

function get_parent_page_permalink($postid) {
  global $wpdb;
  if (isset($postid)) {
    $slide = $wpdb->get_var("SELECT DISTINCT slide_id FROM {$wpdb->prefix}smooth_slider WHERE post_id = {$postid} LIMIT 1");
    if (!empty($parent)) {
      $parent = $wpdb->get_var("SELECT ID FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type = 'post' AND post_content LIKE \"%[smoothslider id='".$slide."']%\"");
      if (!empty($parent)) {
         return get_permalink($parent);
      }
    }
  }
}

That is an even heavier load on the server. You will have two additional queries for each result in the search and, I believe, another query for each get_permalink.

4
  • Wow thank you so much for your reply. I've inserted the code in my theme functions.php but with no success, the search result is the same (returns a list of individual posts). I also have a search.php page, should it be edited too? Sorry, I'm quite a beginner here. Also I assume it's "DISTINCT slider_id" on line 5 in your code?
    – Oh_Franck
    Commented Apr 30, 2013 at 20:50
  • Also shouldn't there be if ( is_search() at the beginning?
    – Oh_Franck
    Commented May 1, 2013 at 7:31
  • This function was meant to grab requests to the single posts and redirect them to them to the parent. Modified a bit you should be able to interrupt the search itself but it is not going to be as simple as what you did in your answer. I'll see what I can do.
    – s_ha_dum
    Commented May 1, 2013 at 13:14
  • Thanks! I can't get that second function to work either; results are the same (single posts). I think I understand what needs to be done, but I have no success so far...
    – Oh_Franck
    Commented May 2, 2013 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.