Ask your WordPress questions! Pay money and get answers fast! (more info)

Custom Loop to return certain Page Template + Custom Fields (Mu)

  • SOLVED

I need a custom loop that will display all pages that use a specific template from across a multisite install and display custom post meta from each page.

I.e.

if template=custom_page.php
return postID
echo custom field 1 for that page()
echo custom field 2 for that page()
echo custom field 3 for that page()


Added:

I found this page http://wp.tutsplus.com/tutorials/plugins/a-featured-blog-plugin-for-wordpress-multisite/ if someone would know how to make that 'return all' for a certain page template to insert in a template

Answers (6)

2012-07-17

Hai Bui answers:

Here you go

global $wpdb;
$table_prefix = $wpdb->base_prefix;

$blog_list = get_blog_list( 0, 'all' );

$template = "Any_Template.php";
$meta_keys = array("'meta_key1'","'meta_key2'","'meta_key3'");

foreach ($blog_list AS $blog) {
echo 'blogid: '.$blog['blog_id'].'<br/>';
$posts = $wpdb->get_col("SELECT ID FROM ".$table_prefix.$blog['blog_id']."_posts a
JOIN ".$table_prefix.$blog['blog_id']."_postmeta b
ON a.ID = b.post_id
WHERE a.post_status = 'publish'
AND a.post_type = 'page'
AND b.meta_key = '_wp_page_template'
AND b.meta_value = '".$template."'");
foreach($posts as $p) {
echo 'post id: '. $p.'<br/>';
$post_meta = $wpdb->get_results("SELECT meta_key, meta_value FROM ".$table_prefix.$blog['blog_id']."_postmeta
WHERE post_id = '".$p."'
AND meta_key IN (" . implode(',',$meta_keys) . ")",OBJECT_K);

print_r($post_meta);
echo '<br/>';
}
}


Kyle comments:

Perfect. Works perfectly and doesn't use switch_to_blog!

2012-07-17

Luis Abarca answers:

Try with

get_posts('meta_key=_wp_page_template&meta_value=custom_page.php');


Kyle comments:

Where would I put the outputted custom fields for each post? like

<?php echo(types_render_field("first", array("raw"=>"false"))); ?>
<?php $rating = myrp_api6_get_rating() * 5; echo myrp_api_star_image($rating); ?>


Luis Abarca comments:

For multisite



$blogs_id = array(1, 2, 3, 4);

$allposts = array();

// store all posts
foreach ($blogs_id as $id) {
switch_blog( $id );
$allposts[$id] = get_posts('meta_key=_wp_page_template&meta_value=custom_page.php');
}

// show posts
foreach ( $allposts as $blogID => $posts ) {
foreach( $posts as $post ) :
setup_postdata($post); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach;
}


Luis Abarca comments:

or this way


$blogs_id = array(1, 2, 3, 4);
$allposts = array();

// store all posts
foreach ($blogs_id as $id) {
// switch to blog to get posts
switch_blog( $id );

$posts = get_posts('meta_key=_wp_page_template&meta_value=custom_page.php');

foreach( $posts as $post ) :
setup_postdata($post); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php echo(types_render_field("first", array("raw"=>"false"))); ?>
<?php $rating = myrp_api6_get_rating() * 5; echo myrp_api_star_image($rating); ?>
<?php endforeach;
}


Kyle comments:

I am not getting any output-I pmed you the page it is being used to see. Also I presume you meant the switch_to_blog() function because switch_blog() was giving an error.

If possible I would prefer to do this without that switch blog function because it is EXTREMELY slow and takes a ton of memory.


Luis Abarca comments:

yep, sorry it's switch_to_blog()

Whats the file name of the custom template ?


Kyle comments:

Profile_Template.php


Luis Abarca comments:

The pages are on multiple blogs isn't it ?, maybe cahing $posts result for a while will do the trick, like creating a cron job and storing results in a serializaed file or temp table


$blogs_id = array(1, 2, 3, 4);
$allposts = array();

// get all posts across multisite
foreach ($blogs_id as $id) {
// switch to blog to get posts
switch_to_blog( $id );

$posts = get_posts('meta_key=_wp_page_template&meta_value=<strong>Profile_Template.php</strong>');

foreach( $posts as $post ) :
setup_postdata($post); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php echo get_post_meta(get_the_ID(), 'custom_field_1', true) ?>
<?php echo(types_render_field("first", array("raw"=>"false"))); ?>
<?php $rating = myrp_api6_get_rating() * 5; echo myrp_api_star_image($rating); ?>
<?php endforeach;
}


Kyle comments:

I'm still not getting any output


Luis Abarca comments:

Check this http://pastebin.com/HgzKbt1W


Kyle comments:

Still no output...

Thanks for the replies by the way! Hopefully this is getting close


Luis Abarca comments:

i can help you if you provide me ftp or login details to check whats happening


Kyle comments:

I would rather not just yet, I still would like to see if anyone has an idea that doesn't use the switch_to_blog() function


Luis Abarca comments:

Add this line to your function to use get_results


global $wpdb;


Kyle comments:

I am still not getting any kind of output


Luis Abarca comments:

Sorry, i mean to add it to the code that Dbranes said


Kyle comments:

I did, here is new paste http://pastie.org/4275332

2012-07-17

Dbranes answers:


you could try to add

restore_current_blog();

at the bottom inside the foreach loop

ps: the function

get_blog_list


is beeing deprecated since 3.0 so maybe you should use something like:


$blogs_id = $wpdb->get_results( $wpdb->prepare("SELECT blog_id, domain, path FROM $wpdb->blogs WHERE public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0' ORDER BY registered DESC"), ARRAY_A );



in your code instead


Kyle comments:

I am getting an error on that line for Fatal error: Call to a member function get_results() on a non-object

2012-07-17

Francisco Javier Carazo Gil answers:

Hi Dbranes,

To use:

$blogs_id = $wpdb->get_results( $wpdb->prepare("SELECT blog_id, domain, path FROM $wpdb->blogs WHERE public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0' ORDER BY registered DESC"), ARRAY_A );

First you have to call:
global $wpdb;


Kyle comments:

That is in the template seen here http://pastie.org/4275332

2012-07-17

seaneswyno answers:

Brand women's national wind lace embroidered chiffon short sleev - $38.00 :

2012-07-17

shapiustakis answers:

下履き