Adding data to wp_sitemeta table - wordpress

Is there a way to add a row to the wp_sitemeta table in Wordpress Multisite? I'm thinking just like for wp_usermeta, there are functions such as add_user_meta( $user_ID, $meta_key, $meta_value), add_user_meta( $user_ID, $meta_key, $meta_value) and similar, there might be a way to do the same for wp_sitemeta?
I have searched, but can't seem to find information about adding to this table anywhere. Can it be done?

WordPress provides the function update_metadata($meta_type, $object_id, $meta_key, $meta_value, [$prev_value]) (see the codex) that can (and probably should) be used for this. It will update the key value if it exists, and add it if it does not exist.
To update wp_sitemeta, set $meta_type = 'site'. The $object_id should be the network ID, which is almost certainly "1". (WordPress only supports a single network with ID "1" via its UI, although the WP DB has schema support for multiple networks.) The $meta_key and $meta_value parameters are for the metadata key and value, respectively.
Note too that wp_sitemeta is for network-wide options, not for blog-specific (child site) options.
If you're able to work at the shell command prompt and have access to the WP-CLI command line client, you can also use the wp network meta update <id> <key> [<value>] [--format=<format>] command. (I have not tested whether this adds or updates as needed, but I expect it does.)

It depends on what you are trying to add. You can use $wpdb
<?php
global $wpdb;
$main_blog_prefix = $wpdb->get_blog_prefix(BLOG_ID_CURRENT_SITE);
$meta_table = $main_blog_prefix."sitemeta";
$insertedRow = $wpdb->insert( $wpdb->prepare(
$meta_table ,
array(
'site_id' => BLOG_ID_CURRENT_SITE,
'meta_key' => 'some_meta',
'meta_value' => "asd asd asda sda sda sd"
)
)
);
?>

Related

Is there any function in Wordpress for set title

I want to know is there any function for the set title in WordPress. Can anyone help me?
As far as I know, there is not easy function for this. You can use the $wpdb object to accomplish this and update the "blogname" option. Definitely use the helper functions and avoid running queries on the database directly. The update helper function forces you to specify "WHERE" clauses, which helps prevent you from completely screwing up your DB.
global $wpdb;
$wpdb->update(
"{$wpdb->prefix}options",
array(
'option_value' => 'New blog title'
),
array(
'option_name' => 'blogname'
),
array(
'%s'
),
array(
'%s'
)
);
I would read up on $wpdb before messing with it, to make sure you are running safe operation. And as always, use a test environment first: https://developer.wordpress.org/reference/classes/wpdb/

Limit wp_query to post_status Publish after the_query has been set

I have some code that displays a specific post type in a list. Some other file (not sure which one) is determining the WP_query $the_query. I want to modify this query to only show post_status = publish.
I have the following code prior to displaying the content:
if ( $the_query->have_posts() ) {
...
I want to filter $the_query to show only post_status = publish. I tried this (just prior to the if statement) without luck:
$the_query->set( 'post_status', 'publish' );
Posts of type sold (a custom post type I added) still show up. How might I modify the_query to only show posts with the post_status set to publish after the_query has been created and set?
Another approach might be just to exclude all posts with the post_status set to sold.
EDIT:
I got my code working by reseting the_query all together (code below). I would still like to know if it is possible to modify the query after the fact. I tried remove_query_arg( $key, $query ), but that didn't work.
Resetting the_query (not the solution I was looking for):
$args = array( 'post_type' => 'CUSTOM_POST_TYPE', 'post_status' => 'publish' );
$the_query = new WP_Query( $args );

WordPress is_search() function always false

I want to add some custom conditions and do some other things on a search WordPress results page. So, I’m checking the value of is_search() to make sure I’m only applying the condition at the right time. So in my child theme's functions.php I put:
if (is_search()) {
...
}
But this always returns false, even with a url like http://mysite/?s=something and my theme's search.php template is being used! Is this not valid to call from functions.php, or am I misunderstanding this function's purpose?
For that matter, looking at a template hierarchy like presented at https://wphierarchy.com, how does WP even know it’s a search results page? How does it know to proceed down the "search results" path? I’ve spent some time perusing the source code but haven’t been able to find the right spot yet.
I tried calling is_search() from search.php, and it works! From some more source code perusal, I discovered that you can't call is_search() from functions.php because functions.php is called too early in the process. Apparently functions.php is considered part of the "WordPress library" and is loaded in wp-blog-header.php line 13, while the template is called in line 19.
In between the two is the wp() function, which sets up the query. That query is actually set up in user.php line lines 1198-1212:
$args = array(
'post_type' => $this->post_type,
'post_name__in' => array( $this->request_type ),
'posts_per_page' => $posts_per_page,
'offset' => isset( $_REQUEST['paged'] ) ? max( 0, absint( $_REQUEST['paged'] ) - 1 ) * $posts_per_page : 0,
'post_status' => 'any',
's' => isset( $_REQUEST['s'] ) ? sanitize_text_field( $_REQUEST['s'] ) : '',
);
...
$requests_query = new WP_Query( $args );
To answer my second question (how does WordPress know it's a search), it’s as simple as having the "s=" query string parameter. Not sure what to do if you want a search result that does not have a search string (say a custom search based solely on pulldowns or something). If you need that, I would recommend looking thru the source file WP-query.php and seeing exactly what you need to pass. Don’t be scared of looking at the source code; It’s very educational!

WooCommerce Queries

Good Day,
I am fairly new to WordPress and WooCommerce.
But I am fairly versed in PHP and MySQL.
So my question is, how do I go about making a specific query for WooCommerce where I can point it to specific meta. For example, if I wanted to make a query to call all the different categories within WooCommerce>
I would normally call it like this;
$args = array(
'number' => 'null',
'orderby' => 'name',
'order' => 'ASC',
'columns' => '4',
'hide_empty' => '1',
'parent' => '',
'ids' => ''
)
Ok so that would be my query, now what I am so struggling with is what do I do with it it now? Cause that part there is what everyone shows, no one tells you or shows you how to actually use the query or where to start.
I have used the basic WP query function, but then the only categories I receive back come from WP and WC.
So how do I point it directly to WC?
Furthermore, these hooks annoy me so. So how do I go about doing it the normal way, bypassing WC and WP functions and build my own query;
$query = "SELECT * FROM wc_categories";
$result = mysql_query($query);
$temp_array = mysqli_fetch_array($result);
That is how I would love to do it, cause no one explains hooks and how to use it, or edit it, everyone assumes that you are pro-leet when it come to familial of WP workings.
So if anyone can point me in the right direction, I would really appreciate it.
And I would love you long time if you could explain to me how to go about my 2nd code block cause that is my preferred way.
Kind Regards,
DK
Creating query using standard wp/wc function is easy. For example if you want to get customer details for specific orders. Required codes might look like this (notice the global function):
global $post, $woocommerce, $the_order;
if ( empty( $the_order ) || $the_order->id != $post->ID )
$the_order = new WC_Order( $post->ID );
$user_info = get_userdata( $the_order->user_id );
$biling_email = $the_order->billing_email;
$phone_no = $the_order->billing_phone;
If you want to query a custom order meta field, you may use a code something like below. You need to know the custom field name in database:
$custom_field = get_post_meta( $post->ID, 'custom_field', true );
For custom query you need to initialize global wpdb function first by adding global $wpdb function before your query. Even this will work with a WP page only, it'll not work with a page created outside WP. Below is an example:
global $wpdb
$table = $wpdb->prefix . 'postmeta'; //Good practice
$orderno = $wpdb->get_results( "SELECT * FROM $table");

Update all the posts at once nothing want to add or delete only update so that post slug get automatically generated..

I have changed the default permalink of my website to Custom permalink (/%post-name%)
Now all posts require slug values .
which is not present currently.A slug is automatically added when a post is published or updated if slug screen option is unable ,but in my case now i have unable the slug option for all post earlier this was disabled and Now I want to update each post nothing want to add or delete just want to update all posts .
Currently number of posts in database is 200000,
Please suggest any efficient query or any method so that my task can be accomplished.
Thanks,
Monika
Try this plugin, it should do the job:
http://www.jerrytravis.com/598/wordpress-plugin-to-generate-post-slugs
Also this chunk of code can do the job, but you have to add some limits to avoid the script to crash due to the max limit of execution:
// get all posts
$posts = get_posts( array ( 'numberposts' => -1 ) );
foreach ( $posts as $post )
{
// check the slug and run an update if necessary
$new_slug = sanitize_title( $post->post_title );
wp_update_post(
array (
'ID' => $post->ID,
'post_name' => $new_slug
)
);
}
Credit for the code:
https://wordpress.stackexchange.com/questions/46086/regenerate-slugs-from-title-of-posts

Resources