WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 18 months ago

Last modified 9 months ago

#22816 closed feature request (maybelater)

Multisite WP_Query

Reported by: ciapci Owned by:
Milestone: Priority: normal
Severity: major Version:
Component: Multisite Keywords: dev-feedback needs-patch
Focuses: Cc:

Description

Multisite has no global search functionality, and suppose I want to show on the home page of my main site posts from all the other sites in my network... I don't have a multisite wp_query functionality. Sure, I can use switch_to_blog, but that only works for results from each blog, one by one, suppose I want navigation on my results, suppose I want to make a most recent posts widget, or any type of functionality where I want to query posts from the entire multisite, custom post types, pages, all the wp_query class has to offer only works for each individual site so far, It would really help as a huge feature for WordPress multisite

Change History (10)

comment:1 @Ipstenu3 years ago

There's a plugin for that, sort of. It collects all posts from all sites onto a 'tags' blog and you can search that. Clicking on the links sends you back to the 'real' source.

http://wordpress.org/extend/plugins/wordpress-mu-sitewide-tags/

The reason there isn't a query across all sites is because of how ginourmous and horrific that would be when you think about it scaling up. Searching all the wp_x_posts tables, combining, sorting, scanning, and then outputting? Yikes.

comment:2 follow-up: @wonderboymusic3 years ago

Pretty much everyone who builds something like this ends up making a join table that has post_ids and blog_ids or makes a bunch of requests, smashes them together, and then stores blog_id, post_id, post_date (or any relevant field you want to sort on) in memory or in a table so that they can paginate.

The idea you propose is definitely a good one but probably doesn't jive with our super-weird schema. I did this exact thing on eMusic for tag archives, here is some relevant code: https://gist.github.com/84da29af561229357f07

comment:3 @scribu3 years ago

  • Cc scribu added

comment:4 in reply to: ↑ 2 @ciapci3 years ago

  • Cc ciapci added
  • Status changed from new to closed

Replying to wonderboymusic:

Pretty much everyone who builds something like this ends up making a join table that has post_ids and blog_ids or makes a bunch of requests, smashes them together, and then stores blog_id, post_id, post_date (or any relevant field you want to sort on) in memory or in a table so that they can paginate.

The idea you propose is definitely a good one but probably doesn't jive with our super-weird schema. I did this exact thing on eMusic for tag archives, here is some relevant code: https://gist.github.com/84da29af561229357f07

Yep, you are correct too, everybody's $wpdb-ing everything they can so that they can get a "close enough" scenario to the desired result I mentioned above. But let's be serious, that is not a solution, that is not even close to being fully compatible with wordpress and a lot os stuff is not going to work with those queries because of that. I've seen the closest thing to a solution here: https://github.com/ericandrewlewis/WP_Query_Multisite , but that script is implemented a little shallow, it does not work properly, but is an interesting approach to making things done. It's a start. Still, this feature is needed and I'm not seeing it pop out on it's own anytime soon,and so I wrote about it here...

comment:5 @ciapci3 years ago

  • Status changed from closed to reopened

comment:6 @jeremyfelt2 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to maybelater
  • Status changed from reopened to closed

Doing this the right way would probably involve at least one new table or place to dump all content so that it could be searched. There are a few other solutions (see Elasticsearch) that are probably better at this kind of thing for other reasons as well.

comment:7 @kokareff18 months ago

  • Keywords dev-feedback needs-patch added
  • Resolution maybelater deleted
  • Status changed from closed to reopened

This question is still actual.

Still there is no way to simply do that:

$the_query = new WP_Query ( array(
                'multisite' => true,
                'cache_results' => false,
                'post_type' => 'post',
                'post_status' => 'publish',
                'posts_per_page' => 10,
                'meta_query' => array(
                    array(
                        'key' => 'general-post',
                        'value' => ""
                    )
                )
            ) );

Even this: https://github.com/ericandrewlewis/WP_Query_Multisite#readme(ericandrewlewis / WP_Query_Multisite) or this https://github.com/miguelpeixe/WP_Query_Multisite(miguelpeixe / WP_Query_Multisite) doesn't work properly.

Also it generating SQL like that:

SELECT SQL_CALC_FOUND_ROWS tables.*
FROM
  (SELECT wp_posts.*,
          '1' AS site_ID
   FROM wp_posts
   INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
   WHERE 1=1
     AND wp_posts.post_type = 'post'
     AND (wp_posts.post_status = 'publish')
     AND ((wp_postmeta.meta_key = 'general-post'
           AND CAST(wp_postmeta.meta_value AS CHAR) = ''))
   GROUP BY wp_posts.ID
   UNION SELECT wp_6_posts.*,
                '6' AS site_ID
   FROM wp_6_posts
   INNER JOIN wp_6_postmeta ON (wp_6_posts.ID = wp_6_postmeta.post_id)
   WHERE 1=1
     AND wp_6_posts.post_type = 'post'
     AND (wp_6_posts.post_status = 'publish')
     AND ((wp_6_postmeta.meta_key = 'general-post'
           AND CAST(wp_6_postmeta.meta_value AS CHAR) = ''))
   GROUP BY wp_6_posts.ID
   UNION SELECT wp_7_posts.*,
                '7' AS site_ID
   FROM wp_7_posts
   INNER JOIN wp_7_postmeta ON (wp_7_posts.ID = wp_7_postmeta.post_id)
   WHERE 1=1
     AND wp_7_posts.post_type = 'post'
     AND (wp_7_posts.post_status = 'publish')
     AND ((wp_7_postmeta.meta_key = 'general-post'
           AND CAST(wp_7_postmeta.meta_value AS CHAR) = ''))
   GROUP BY wp_7_posts.ID
   UNION SELECT wp_8_posts.*,
                '8' AS site_ID
   FROM wp_8_posts
   INNER JOIN wp_8_postmeta ON (wp_8_posts.ID = wp_8_postmeta.post_id)
   WHERE 1=1
     AND wp_8_posts.post_type = 'post'
     AND (wp_8_posts.post_status = 'publish')
     AND ((wp_8_postmeta.meta_key = 'general-post'
           AND CAST(wp_8_postmeta.meta_value AS CHAR) = ''))
   GROUP BY wp_8_posts.ID) tables
ORDER BY tables.post_date DESC LIMIT 0,
                                     10

(it doesn't work :-( )

Well, we have redesigned admin, new shitty default theme, even rebranded Wordpress.org. But functionality of Multisite mode still weird and '04-style.

comment:8 @nacin18 months ago

  • Resolution set to maybelater
  • Status changed from reopened to closed

Hi kokareff: Multisite was designed for each site to be its own island. We're not into changing the schema and have no plans to implement global search in core (hence "maybe later"). If you need that, consider a non-MySQL solution — it's the only thing that is going to scale anyway. Search in MySQL sucks even without those UNION queries.

comment:9 @SergeyBiryukov15 months ago

#27701 was marked as a duplicate.

comment:10 @marsjaninzmarsa9 months ago

Please reopen it, for today only solution is http://codex.wordpress.org/WPMU_Functions/switch_to_blog, but

This switch is to be used for internal and admin area functions. It's too expensive a query to run on the front end.

:<

Note: See TracTickets for help on using tickets.