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

Urgent multi site custom loop to check if posts with a custom... WordPress

  • SOLVED

<strong>Urgent multi site custom loop to check if posts with a custom field value exist on the site.
</strong>

This $30 is a deposit - I will double it all for first person who offers a perfect solultion.


Hello,

I need help with a loop/query that checks all my multisite blogs (except blog #1)

I need this loop to simply do this...

<strong>

1. Loop through all my blogs and output in DSC order... #9 #8 #7 #6 #5 #4 #3 #2

2. Ignore the main blog - blog #1.

3. The blogs must be active - please ignore in-active/deleted blogs.

4. When the loop gets to the blog, I would like it to check all posts in my custom post type for a custom field. It needs to compare this custom field value with the current user name. These posts are also private.

5. If the username does not match this custom field, then the loop outputs nothing.

6. If the username does match the custom field string, then I would like the loop to output the site information for that current blog being checked.

</strong>


My custom field key is called <strong>login_name</strong>

Please see my query below which might help you create this amazing multisite loop. You can see which custom field to use and how to compare it. And the post type information...


$press = new WP_Query(array(
'posts_per_page' => 1,
'post_type' => 'individual',
'post_status' => 'private',
'meta_query' => array(
array(
'key' => 'login_name',
'value' => $current_user->user_login,
'compare' => '='
)
)
));



The loop output...



<div class="span4 column">

<div class="well well-medium well-event" id="well-welcome-<?php echo $network_info->customcode; ?>">

<h3 class="text-center"><?php echo $network_info->blogevent; ?></h3>

<a href="<?php echo $network_info->siteurl; ?>" title="View Downloads" class="btn btn-large btn-block"><strong><?php echo $network_info->blogshortname; ?></strong></a>

</div>

</div>




Also I have another question to go with this loop. Is it possible for me to create extra site meta information.

Currently the only site information I can get is this...

<em>The code below is the output of this <strong>print_r( $network_info );</strong></em>


stdClass Object ( [blog_id] => 1 [site_id] => 1 [domain] => mywordpress.com [path] => / [registered] => 2013-02-15 18:51:54 [last_updated] => 2013-04-08 22:28:18 [public] => 0 [archived] => 0 [mature] => 0 [spam] => 0 [deleted] => 0 [lang_id] => 0 [blogname] => My Wordpress Network [siteurl] => http://mywordpress.com [post_count] => )


I need some extra blog meta fields that I can some how output in my custom loop.

Is it possible to almost create like custom fields for my sites? Which will be out putted in my loop above.

For example like this...


[blogshortname]
[blogevent]
[blogdescription]
[customcode]





Can anyone please help me with this.

Please see this code diagram below of how I envisage this loop to to work...


<?php

// LOOP BEGINS

// GET THE BLOG (get the last blog first the check in descending order)

// CHECK THE CUSTOM POST TYPE = 'individual' FOR POSTS WITH STATUS = 'private' && IF CUSTOM FIELD 'login_name' IS EQUAL TO '$current_user->user_login'

// IF IS FALSE DO NOTHING

// IF IS TRUE...

// GET THE BLOG INFORMATION AND OUTPUT IT

// END WHILE

// LOOP TO THE NEXT BLOG - REPEAT THE ABOVE

// END IF

?>

Answers (1)

2013-04-26

Daniel Yoen answers:

base on here : http://wordpress.stackexchange.com/a/50550, you can add custom blog meta by add this line :

add_action('signup_blogform', 'add_extra_field_on_blog_signup');
function add_extra_field()
{ ?>
<label>Blog Shortname</label>
<input type="text" name="blogshortname" />
<label>Blog Event</label>
<input type="text" name="blogevent" />
<label>Blog Description</label>
<input type="text" name="blogdescription" />
<label>Custom Code</label>
<input type="text" name="customcode" />
<?php }

add_filter('add_signup_meta', 'append_extra_field_as_meta');
function append_extra_field_as_meta($meta)
{
if(isset($_REQUEST['blogshortname']))
{
$meta['blogshortname'] = $_REQUEST['blogshortname'];
}
if(isset($_REQUEST['blogevent']))
{
$meta['blogevent'] = $_REQUEST['blogevent'];
}
if(isset($_REQUEST['blogdescription']))
{
$meta['blogdescription'] = $_REQUEST['blogdescription'];
}
if(isset($_REQUEST['customcode']))
{
$meta['customcode'] = $_REQUEST['customcode'];
}
return $meta;
}

add_action('wpmu_new_blog', 'process_extra_field_on_blog_signup', 10, 6);
function process_extra_field_on_blog_signup($blog_id, $user_id, $domain, $path, $site_id, $meta)
{
update_blog_option($blog_id, 'blogshortname', $meta['blogshortname']);
update_blog_option($blog_id, 'blogevent', $meta['blogevent']);
update_blog_option($blog_id, 'blogdescription', $meta['blogdescription']);
update_blog_option($blog_id, 'customcode', $meta['customcode']);

}


hope this help :-)


Josh Cranwell comments:

Awesome - thanks Daniel. I was looking for ages for something like this for ages in google.

When the question ends I will add you more funds for this as separate :-)


Dont suppose you know make such a multisite loop - or if it's possible?


Daniel Yoen comments:

I think, loop is looks like this(not tested) :

$blogs = get_last_updated();
foreach ($blogs as $blog)
{

switch_to_blog($blog["blog_id"]);

$lastposts = get_posts(array(
'posts_per_page' => 1,
'post_type' => 'individual',
'post_status' => 'private',
'meta_query' => array(array('key' => 'login_name', 'value' => $current_user->user_login, 'compare' => '='))));

foreach($lastposts as $post) :
setup_postdata($post); ?>

<div class="span4 column">
<div class="well well-medium well-event" id="well-welcome-<?php echo $network_info->customcode; ?>">
<h3 class="text-center"><?php echo $network_info->blogevent; ?></h3>
<a href="<?php echo $network_info->siteurl; ?>" title="View Downloads" class="btn btn-large btn-block"><?php echo $network_info->blogshortname; ?></a>
</div>
</div>

<?php endforeach;
restore_current_blog();
}


Josh Cranwell comments:

I've added this can't find where the label and inputs are meant to appear.

I've looked in the settings tab in the network but they do not appear. Where can find these inputs?

Thanks


Josh Cranwell comments:

Ah sweet just saw your loop.

I will test it now and let you know.

Thanks soo much


Daniel Yoen comments:

for backend, you need "add_settings_field" instead "signup_blogform". I will test and let you know soon :-)


Josh Cranwell comments:

What like this - I still cant seem to find the option.

Where would these setting be...


add_action('signup_blogform', 'add_extra_field_on_blog_signup');
add_action('add_settings_field', 'add_extra_field_on_blog_signup');
function add_extra_field()
{ ?>

<label>Blog Shortname</label>
<input type="text" name="blogshortname" />

<label>Blog Event</label>
<input type="text" name="blogevent" />

<label>Blog Description</label>
<input type="text" name="blogdescription" />

<label>Custom Code</label>
<input type="text" name="customcode" />

<?php }


Josh Cranwell comments:

Here are gists of the what you have done...

But nether seem to being doing anything...


This is my loop in test.php template.
https://gist.github.com/jcranny/7ec5535dc8867dba4609


This is my functions.php
https://gist.github.com/jcranny/bf9dd25b2ad59e2bc184


Can you help?


Daniel Yoen comments:

here tested script for add custom field :

add_action('admin_print_scripts-site-new.php', "admin_script");
function admin_script()
{
wp_register_script("dan-custom-script", get_template_directory_uri() . "/js/script.js"); //adjust this script location
wp_enqueue_script("dan-custom-script");
}

add_action('wpmu_new_blog', 'add_new_blog_field');
function add_new_blog_field($blog_id, $user_id, $domain, $path, $site_id, $meta)
{
$new_field_value = 'default';
if (!empty($_POST['blog']['blogshortname']))
{
switch_to_blog($blog_id);
$new_field_value = $_POST['blog']['blogshortname'];
add_blog_option($blog_id, 'blogshortname', $new_field_value);
restore_current_blog();
}
if (!empty($_POST['blog']['blogevent']))
{
switch_to_blog($blog_id);
$new_field_value = $_POST['blog']['blogevent'];
add_blog_option($blog_id, 'blogevent', $new_field_value);
restore_current_blog();
}
if (!empty($_POST['blog']['blogdescriptions']))
{
switch_to_blog($blog_id);
$new_field_value = $_POST['blog']['blogdescriptions'];
add_blog_option($blog_id, 'blogdescriptions', $new_field_value);
restore_current_blog();
}
if (!empty($_POST['blog']['customcode']))
{
switch_to_blog($blog_id);
$new_field_value = $_POST['blog']['customcode'];
add_blog_option($blog_id, 'customcode', $new_field_value);
restore_current_blog();
}
}
function arrayPushAfter($src, $in, $pos)
{
if (is_int($pos))
$R = array_merge(array_slice($src, 0, $pos + 1), $in, array_slice($src, $pos + 1));
else
{
foreach ($src as $k => $v)
{
$R[$k] = $v;
if ($k == $pos)
$R = array_merge($R, $in);
}
}
return $R;
}

add_action('manage_blogs_custom_column', 'add_columns', 10, 2);
add_action('manage_sites_custom_column', 'add_columns', 10, 2);
function add_columns($column_name, $blog_id)
{
if ('blog_id' === $column_name)
echo $blog_id;
elseif ('blogshortname' === $column_name)
{
$blogshortname = get_blog_option($blog_id, 'blogshortname');
echo $blogshortname;
}
elseif ('blogevent' === $column_name)
{
$blogevent = get_blog_option($blog_id, 'blogevent');
echo $blogevent;
}
elseif ('blogdescriptions' === $column_name)
{
$blogdescriptions = get_blog_option($blog_id, 'blogdescriptions');
echo $blogdescriptions;
}
elseif ('customcode' === $column_name)
{
$customcode = get_blog_option($blog_id, 'customcode');
echo $customcode;
}
return $column_name;
}

add_filter('wpmu_blogs_columns', 'print_columns');
function print_columns($cols)
{
$in = array("blog_id" => "ID");
$cols = arrayPushAfter($cols, $in, 0);
$cols['blogshortname'] = __('Blog Shortname');
$cols['blogevent'] = __('Blog Event');
$cols['blogdescriptions'] = __('Blog Description');
$cols['customcode'] = __('Custom Code');
return $cols;
}


script.js

(function($) {
$(document).ready(function() {

$('<tr class="form-field form-required"></tr>').append(
$('<th scope="row">Blog Shortname</th>')
).append(
$('<td></td>').append(
$('<input class="regular-text" type="text" title="Blog Shortname" name="blog[blogshortname]">')
).append(
$('<p></p>')
)
).insertAfter('#wpbody-content table tr:eq(2)');

$('<tr class="form-field form-required"></tr>').append(
$('<th scope="row">Blog Event</th>')
).append(
$('<td></td>').append(
$('<input class="regular-text" type="text" title="Blog Event" name="blog[blogevent]">')
).append(
$('<p></p>')
)
).insertAfter('#wpbody-content table tr:eq(3)');

$('<tr class="form-field form-required"></tr>').append(
$('<th scope="row">Blog Description</th>')
).append(
$('<td></td>').append(
$('<input class="regular-text" type="text" title="Blog Description" name="blog[blogdescriptions]">')
).append(
$('<p></p>')
)
).insertAfter('#wpbody-content table tr:eq(4)');

$('<tr class="form-field form-required"></tr>').append(
$('<th scope="row">Custom Code</th>')
).append(
$('<td></td>').append(
$('<input class="regular-text" type="text" title="Custom Code" name="blog[customcode]">')
).append(
$('<p></p>')
)
).insertAfter('#wpbody-content table tr:eq(5)');


});
})(jQuery);


and result :