How can I display posts from the other sites in a WordPress multisite setup? - wordpress

I have a small network of sites setup with WordPress 3.0's multisite feature. I would like to create another site which pulls certain posts from the various other sites to display. This new 'hub' site would seem like its own separate site to the user (with domain mapping), but its content is coming from the posts from the other sites.
How can I get posts from another site in a WordPress multisite setup? Can I query for posts based on the name of the site? The end result needs to be a collection of posts from the different sites sorted by date.
Thanks for your help.

I had the similar issue where I wanted to get posts from one blog and display it on an other I came up with the following solution which you could modify slightly to meet your needs if needed
<?php
global $switched;
switch_to_blog(2); //switched to 2
// Get latest Post
$latest_posts = get_posts('category=-3&numberposts=6&orderby=post_name&order=DSC');
$cnt =0;?>
<ul>
<?php foreach($latest_posts as $post) : setup_postdata($post);?>
<li>
<?php echo short_title('...', 7); ?>
</li>
<?php endforeach ; ?>
<?php restore_current_blog(); //switched back to main site ?>
I'm also limiting the amount of words which is being out putted if you do not want this feature simple use
$post->post_title;
Hope it helps.

This wouldn't be terribly difficult to set up with direct database calls. You can query posts from any site on the install with the $wpdb object. See Displaying Posts Using a Custom Select Query for information on using a custom database query, but keep in mind that instead of selecting from $wpdb->posts you're going to need to access the specific site table you want posts from. On a default Wordpress 3 install, this would be wp_12_posts where 12 is the site id. The id can be found in the wp_blogs table, or by looking at the ID column in the Sites section of the admin menu.

Related

How to create a profile page in Wordpress Multisite

I can't find a way to create a front-end profile page for all users under Wordpress Multisite Network. I also wanted the profile page to be on the main site and not on sub-sites if possible. I don't want to use any social network plugins like buddypress.. I just want to create a page like http://mainsite.com/profile/username
Is there a way to accomplish this?
Maybe you might check out Buddypress which is a social layer on top of Wordpress and is able to do what you need (i.e. something like http://mainsite.com/profile/username).
Link: http://buddypress.org/
More specifically, in Buddypress the default behaviour is http://mainsite.com/members/username
I have noticed that you have edited the question and that you are looking for a way to accomplish this without any plugin. There are a series of tags in Wordpress to show the author's data. So basically you could create a page and then use the_author_meta tags. For example the following code echoes the first and last name of a user whose name is stored in $userID:
<?php
$pagename = $userID;
echo the_author_meta(user_firstname, $userID );
echo the_author_meta(user_lastname, $userID );
?>
Please note that the variable $pagename is empty in case you use a static page as home page, but it is not your case.
Source: https://codex.wordpress.org/Template_Tags/the_author_meta

Wordpress permalink from the database?

I am trying to access selected post information from my WordPress database to display on a second website which is not a WordPress site nor is it php, it is in fact an asp.net website that i want to display the information on.
I have a WordPress blog at blog.domain.com and another website at domain.com both running on different servers and I need to display the excerpt, title with link on my asp.net website for selected posts. I can easily grab the excerpt and title from the blog's database but the only problem is that the permalink is not in the database.
I had the idea that maybe if I place a short piece of code on every blog to insert the permalink to a new table, i could then grab that to use over on the other website. But that didnt work, perhaps I am not doing it right. This is the code I used;
<?php
global $wpdb;
$table_name = $wpdb->prefix . "posts_pages_url";
$wpdb->insert($table_name , array('post_id' => the_ID(), 'url' => the_permalink()));
?>
Some might say, "why dont you just use an RSS Feed?" Well if it comes down to it, I just might have to, but i would prefer not to as connecting to the database directly to access my selected posts is much more flexible.
I would be very grateful if anyone had any suggestions on how I can access the permalink via a database call.
Many thanks
You could use get_permalink() in your code, or link to the guid value in the DB - not ideal I know, but it should at least work.
EDIT:
Same goes for the_ID() - it should be changed to get_the_ID().
This code will get it for you if using Yoast
$q = "SELECT * FROM wp_yoast_indexable yi
WHERE object_type = 'post'
AND object_id = '$ref'";
$yis = $scDb->select($q);
$permaLink = "";
if(isset($yis[0]))
{
$permaLink = $yis[0]->permalink;
}

Wordpress search results on external page

I have a custom search engine on a non-wordpress page. This search engine searches a database for a city (specified by a text input) and then returns the relevant information about that city and displays it on the page.
What I want to do now, is to below those results, display the results of a wordpress search of the same term on a blog that resides on the same server/domain. So basically I want to show the same results that a wordpress search of that keyword would return, but I want to display them on a non-wordpress page, not in the blog's theme.
What I have:
a variable holding a search term, the search term has already been shampooed and conditioned to be search engine friendly.
What I don't want to do
I don't want to use an iframe and have the blog template be displayed on the page.
I can't have a secondary search box. I need to somehow use the value of the variable that I have, I can't ask the user to submit a second form or anything.
My ideas so far
Is there a way to run the wordpress search function and grab the data that it returns? I've gotten as far as including wp_blog_header.php on my static page so that I can make use of the wordpress functions.
Or would it be better to write a function that duplicates what the wordpress search does on the wordpress databases, but returns the data in a way that I can use?
Or is there a different approach that I should take for this that I've overlooked? Thanks!
You must first include the Wordpress files
<?php define('WP_USE_THEMES', false);
require('/server/path/to/your/wordpress/site/htdocs/blog/wp-blog-header.php');
then use query_posts and the loop
query_posts('s='.keyword);
while (have_posts()): the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<p>Read more...</p><?php
endwhile;

WordPress--post only latest 1 or 2 or 3 posts on homepage

How do I just display the latest post on home (not the whole archive as wp does by default)?
And how can I influence that it has/not date, category, read more, etc?
WP manual does not explain this clearly. Or does it? If yes, please tell me where?
Thank you guys! And girls!
The new Wordpress Themes have child themes, it is a new way to organize and simplify upgrading themes, but it can be a pain because they are not as easy to modify as older themes. Anyway, there is the old way:
You can use the settings in the Administration Page to set your homepage to display only one post (Settings / Reading / Blog pages show at most -> 1 post). But if you want just your home to display one post, and decide you want your archives to display the basic 10 posts, you can edit your theme template to use this function:
<?php query_posts('posts_per_page=1'); ?>
Function Reference/query posts
http://codex.wordpress.org/Function_Reference/query_posts
To be sure no date or category is displayed, you also have to edit the template and remove the corresponding php code (in the loop, you should look for these functions:
<?php the_category(); ?>
<?php the_date(); ?>
Function Reference/the category
http://codex.wordpress.org/Function_Reference/the_category
Function Reference/the date
http://codex.wordpress.org/Function_Reference/the_date
To use the new way, you have to create a child theme to your theme, and override the loop by creating your own loop.
Chld Themes
http://codex.wordpress.org/Child_Themes

WordPress Business Directory - best approach

I want to implement a business directory in WordPress and I am looking for feedback on the best approach:
I have a categories and a businesses table
Do I create a page for every business together with it's category relationship
Do I create a page and assign it a template?
What are the ups and downs with each approach?
Looking for some answers from people that might of already done this and can speak from experience
I've been looking for a similar thing and found this yesterday... http://www.directorypress.net/
It's not cheap but it looks like it might work.
The nice thing about WordPress is that you can go a few different routes w/ trying to accomplish this.
One route that's worked well for me is to list each business as a post and then create categories and sub-categories to house these businesses. Once this is done, you can create custom templates for either category templates (so if your have a category named 'Food Establishments' and it has the category id of 5, you can create a template named category-5.php and then WordPress will know to look to that template first and then move on to the regular category.php file and so on).
In your category template you can then list out each sub category w/ a function like wp_list_categories(); and call out the children categories from there.
The other option is to still go the route of entering in your businesses as a separate post, filing them under the appropriate categories. But instead of creating a category template, you can create a page template. You'll need to do a few things for this
Pre-prend your template file w/ the necessary template syntax to tell WordPress to look for that as a template file such as:
<?php
/* Template Name: Food Establishments */
?>
Create a page in WordPress using this new template
In your page template create a new query, pointing to this category, such as:
<?php $business_food = new WP_Query( 'cat=5' ); ?><br />
<?php if ( $business_food->have_posts() ) : while ( $business_food->have_posts()) : $business_food->the_post(); ?><br />
<?php the_content(); // and do other stuff here ?><br />
<?php endwhile; endif; ?><br />
There are other options you can do as well, and for those, I would hang out some WordPress user groups to see if you can grab some other ideas.

Resources