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

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

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

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

This question has been answered.

ktrusak | 07/17/12 at 3:39pm Edit

Previous versions of this question: 07/17/12 at 8:24pm

The experts have suggested, on average, a prize of $75 for this question.

(6) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    07/17/12
    4:03pm
    Luis Abarca says:

    Try with


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

    Previous versions of this answer: 07/17/12 at 4:03pm

    • 07/17/12 4:09pm

      ktrusak says:

      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); ?>

    • 07/17/12 4:10pm

      Luis Abarca says:

      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;
      }

    • 07/17/12 4:12pm

      Luis Abarca says:

      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;
      }

    • 07/17/12 4:18pm

      ktrusak says:

      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.

    • 07/17/12 4:26pm

      Luis Abarca says:

      yep, sorry it's switch_to_blog()

      Whats the file name of the custom template ?

    • 07/17/12 4:27pm

      ktrusak says:

      Profile_Template.php

    • 07/17/12 4:33pm

      Luis Abarca says:

      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=Profile_Template.php');

      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;
      }

    • 07/17/12 4:36pm

      ktrusak says:

      I'm still not getting any output

    • 07/17/12 5:58pm

      Luis Abarca says:

      Check this http://pastebin.com/HgzKbt1W

    • 07/17/12 6:03pm

      ktrusak says:

      Still no output...

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

    • 07/17/12 6:38pm

      Luis Abarca says:

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

    • 07/17/12 8:18pm

      ktrusak says:

      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

    • 07/17/12 9:49pm

      Luis Abarca says:

      Add this line to your function to use get_results


      global $wpdb;

    • 07/17/12 9:59pm

      ktrusak says:

      I am still not getting any kind of output

    • 07/17/12 10:04pm

      Luis Abarca says:

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

    • 07/17/12 10:06pm

      ktrusak says:

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

  • avatar
    Last edited:
    07/17/12
    6:42pm
    Dbranes says:


    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

    • 07/17/12 8:19pm

      ktrusak says:

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

  • avatar
    Last edited:
    07/18/12
    3:15am
    Francisco Javier Carazo Gil says:

    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;

    • 07/18/12 9:04am

      ktrusak says:

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

  • avatar
    Last edited:
    07/18/12
    5:47am

    This answer was downvoted by the top experts.

  • avatar
    Last edited:
    07/18/12
    11:21am

    This answer was downvoted by the top experts.

  • avatar
    Last edited:
    07/18/12
    2:04pm
    Hai Bui says:

    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/>';
    }
    }

    • 07/18/12 2:08pm

      ktrusak says:

      Perfect. Works perfectly and doesn't use switch_to_blog!

This question has expired.



ktrusak voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.