I am using cpt-archive plugin to generate archive by month in my wordpress post. Here the plugin using this code,
$query = "SELECT YEAR( post_date ) AS `year`,
MONTH(post_date) AS `month`,
count(ID) as posts
FROM $wpdb->posts $join $where
GROUP BY YEAR(post_date),
MONTH(post_date)
ORDER BY post_date DESC $limit";
$arcresults = $wpdb->get_results($query);
Here it's working fine with post_date. I need to customize this to get archived by meta fields. I have two meta keys such as _event_start_date and _event_end_date.
edit
now i am getting all the custom post type months based on the _event_end _date with the following query
$months = $wpdb->get_results("SELECT DISTINCT YEAR(meta_value) AS year, MONTH(meta_value) AS month FROM $wpdb->postmeta WHERE meta_key = '_event_end_date' WHERE meta_value <= now( )ORDER BY meta_value ASC");
but i want only past months only. how to do change this query for getting only archived months.
Why don't you use wp_get_archives?
Something like this
<?php wp_get_archives( array( 'type' => 'monthly', 'meta_value' => array('_event_start_date', '_event_end_date') ) ); ?>
Related
I have a woocommerce store and I try to select from Mysql all products and set them as "Featured" products or set them as Non "Featured" products.
I have found the following query that set post_status to whatever I want. I quess that I just have to replace post_status to something else that set products as "featured" products or as no "featured" product.
UPDATE wp_posts
SET post_status = 'Published'
AND post_type = 'product'
OR post_type = 'product_variation'
Is there a reason you want to hack this into the MySQL database directly? The proper approach would be to use the WC API in order to do so.
An example that uses an array of WC products:
foreach ( $products as $product ) {
$productObj = $product->get_product();
$productObj->set_featured(true);
$productObj->save();
}
You can use following custom PHP code:
<?php
error_reporting(E_ALL);
define( 'WP_USE_THEMES', false ); // Don't load theme support functionality
require( './wp-load.php' );
//echo $end_datenew;
global $wpdb;
global $woocommerce;
// The SQL query
$results = $wpdb->get_results( "
SELECT p.ID as product_id
FROM {$wpdb->prefix}posts as p
WHERE p.post_type LIKE 'product'
AND p.post_status LIKE 'publish'
AND p.post_parent = '0'
" );
foreach($results as $result)
{
$wpdb->insert('h3wp_term_relationships', array(
'object_id' => $result->product_id,
'term_taxonomy_id' => '20',
'term_order' => '0', // ... and so on
));
}
I am trying to query some data onto my page on WordPress, but I am not sure where to actually put my SQL function. It has been driving me crazy for 2 days and have not found out an answer. I've look through my phpadmin, wp-db.php .. Am I missing something here? Could somebody please post the very first steps on getting this started..? I have experience in writing code and SQL, so if I just know where to put my code in I could get started. Thank you so much for your help.
You can use WordPress Query to fetch data from your database . Instead of writing SQL query, you can fetch from $wpdb . query_posts() or WP_Query() are the predefined functions in WordPress . For fetching all pages, you can just use : query_posts('post_type=page'); similarlly posts query_posts('post_type=post');
Other conditions can be specified in the args section , ie for particular page name :
query_posts('post_type=page&post_name=contact');
Thank You
Here is the simple example query:
<?php
global $wpdb;
global $post;
$querystr = "
SELECT DISTINCT wposts.*
FROM $wpdb->posts wposts
LEFT JOIN $wpdb->postmeta wpostmeta ON wposts.ID = wpostmeta.post_id
LEFT JOIN $wpdb->term_relationships ON (wposts.ID = $wpdb-
>term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON ($wpdb-
>term_relationships.term_taxonomy_id = $wpdb-
>term_taxonomy.term_taxonomy_id)
WHERE wpostmeta.meta_key = 'customDateField'
AND wpostmeta.meta_value >= CURDATE()
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->term_taxonomy.term_id IN(1,2)
ORDER BY wpostmeta.meta_value ASC
LIMIT 4
";
?>
Use WP_QUERY class for your queries. You can write the code in template files of your theme.
$query = new WP_Query( array( 'post_type' => 'page', 'post__in' => array( 2, 5, 12, 14, 20 ) ) );
Here you will find examples and documentation.
https://codex.wordpress.org/Class_Reference/WP_Query
for custom queries use wpdb class:
https://codex.wordpress.org/Class_Reference/wpdb
// 1st Method - Declaring $wpdb as global and using it to execute an SQL query statement that returns a PHP object
global $wpdb;
$results = $wpdb->get_results( 'SELECT * FROM wp_options WHERE option_id = 1', OBJECT );
// 2nd Method - Utilizing the $GLOBALS superglobal. Does not require global keyword ( but may not be best practice )
$results = $GLOBALS['wpdb']->get_results( 'SELECT * FROM wp_options WHERE option_id = 1', OBJECT );
Im trying to make what seems like a simple adjustment to a plugin im using, unfortunately im just a 'wannabe programmer' and not the real deal so i wa shoping someone whom is a real programmer can help me out here. If you can help me i would be foreever grateful and even willing to pay a bit for someone to explain this process to me...... here goes:
Im working with a plugin, it has a list of posts recently created and shows ALL posts no matter which user i login with... I would like to change this to show ONLY post by the user whom is logged in... below is the file\
<?php
global $wpdb;
$current_time = current_time('mysql') + 60;
$ereminder_array = $wpdb->get_results( $wpdb->prepare("
SELECT *
FROM {$wpdb->posts}
WHERE post_date <= %s
AND post_type ='ereminder'
AND post_status = 'draft'
AND author = 'wp_current_user'
ORDER BY post_date ASC
", $current_time) );
$scheduled_data = array(
'list' => $ereminder_array,
'type' => 'scheduled'
);
echo PDER_Utils::get_view( 'ereminder-list.php', $scheduled_data );
?>
if anybody can tell me how to alter above code to show only post by the user that is logged in instead of all the users like it does now i would be forever grateful.
thanks in advance
Try this,
<?php
$current_user = wp_get_current_user();
global $wpdb;
$current_time = current_time('mysql') + 60;
$ereminder_array = $wpdb->get_results( $wpdb->prepare("
SELECT *
FROM $wpdb->posts
WHERE post_date <= %s
AND post_type ='ereminder'
AND post_status = 'publish'
AND post_author = %d
ORDER BY post_date ASC
", $current_time, $current_user->ID) );
$scheduled_data = array(
'list' => $ereminder_array,
'type' => 'scheduled'
);
echo PDER_Utils::get_view( 'ereminder-list.php', $scheduled_data );
?>
I need to show a list of Wordpress categories only if the category name begins with an "*" (asterisk).
I know I can display the list of categories based on some criteria using the template tags:
<?php wp_list_categories( $args ); ?>
What I don't know how to do is write the if statement that checks to see if the category name starts with an *.
I think, you may try this
global $wpdb;
$query = SELECT GROUP_CONCAT( wp_terms.term_id ) AS IDS FROM wp_term_relationships LEFT JOIN wp_term_taxonomy ON ( wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id ) LEFT JOIN wp_terms ON wp_term_taxonomy.term_taxonomy_id = wp_terms.term_id WHERE wp_term_taxonomy.taxonomy = 'category' AND wp_terms.name LIKE '*%'
$ids = $wpdb->get_var($query);
wp_list_categories("include=$ids");
Currently i have 2 categories and 1 custom field
Category = CAT01 and CAT02 / ID's 1 and 2
Custom Field = Test01
What I want is the average of all values entered in my custom field (Test01), but from 1 category.
I'm new to Wordpress, but I got the following:
$wp_query = new WP_Query( array (
'category_id' => '1',
'meta_key' => 'Test01',
)
);
Without the category id it seems to do something, but not what I want.
Can anyone help me out with this, really appreciate the effort :)
I think using WP_Query adds needless overhead to this. I would do a custom SQL query (although you'd have to keep an eye on if WP changes their taxonomy DB structure in the future).
It would look something like this:
global $wpdb;
$term = get_term(1, 'category'); //need the term_taxonomy_id, not always the same as term_id
$sqlQuery = "SELECT AVG(meta_value) FROM $wpdb->postmeta WHERE meta_key = 'Test01' AND post_id IN(SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = '" . $term->term_taxonomy_id . "') ";
$avg = $wpdb->get_var($sqlQuery);