I am trying to get the value of meta_value field from current post.
<?php
$cupost = $post->ID;
$registros = $wpdb->get_results( 'SELECT meta_value FROM auto_postmeta
WHERE post_id = $cupost AND meta_key="mg_game"');
echo $registros[0]->meta_value . "<br/>";
?>
It does not shows the echo from $registros.
But if i put directly on the query the number of the post (post_id = 7), it shows the value of meta_value.
Thy this out:
<?php
$cupost = $post->ID;//First check if this variable gets the id, i would echo the $cupost variable to check.
$registros = $wpdb->get_results( "SELECT meta_value FROM auto_postmeta WHERE post_id = ".$cupost." AND meta_key= 'mg_game' ");
echo $registros[0]->meta_value . "<br/>";
?>
You have to concatenate the Query String with the Php Variables
Related
How to get all the posts in wordpress using single query.
I have tried below using fetchAll(), but its not working.
It just displays RESUlT as many times there are total posts.
Code:
UPDATED Helper file:
$posttypevalue = Mage::helper('wordpress')->getPostMetaData();
var_dump($posttypevalue); //gives NULL
Template file
public function getPostMetaData()
{
try{
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('new_db');
//$query = 'SELECT meta_value FROM ' . $resource->getTableName('wp_postmeta'). ' WHERE post_id = '.$postID.' and meta_key = "type"';
echo 'vardumping results'; //Even this line is not displaying
$query = 'SELECT wp_posts.*, wp_postmeta.meta_value
FROM wp_posts ,wp_postmeta
WHERE wp_posts.ID =wp_postmeta.post_id
AND wp_postmeta.meta_key="type"
AND wp_posts.post_status="publish"';
$results = $readConnection->fetchAll($query);
//$postdata = array();
//foreach($results as $value){
//var_dump($value); //gives empty
//$postdata[]= $value['post_title'];
//}
}catch (Exception $e) {
return true;
}
}
DB structure is as:
Output:
For the 1st query which is commented:
$query = 'SELECT meta_value FROM ' . $resource->getTableName('wp_postmeta'). ' WHERE post_id = '.$postID.' and meta_key = "type"'
vardumping results
1 displayed 5 times
For the query i am using currently:
Output for var_dump($values):
Empty page with header and footer.
Even the echo above the line $query doesnt display.
It very weird!!
You can do a var_dump($value) to see the content.
I think you should use $postdata[] = $value['post_title'];.
The field names of the results don't contain the table name.
Why the result return is null? What is wrong? If I use this consult on directly phpmyadmin, the result is correctly.
<?php $postid = get_the_ID(); ?>
<?php
$get_thumb = $wpdb->get_var
( "SELECT meta_value FROM `wp_postmeta` WHERE `post_id` = $postid AND `meta_key` = '_wp_attached_file'" );
echo "<p>Thumb URL: {$get_thumb}</p>";
?>
I'd suggest using the $wpdb->prepare() statement to ensure the SQL query is generated correct and the $get_thumb variable is correctly set in the query.
$postid = get_the_ID();
$get_thumb = $wpdb->get_var($wpdb->prepare(
"SELECT meta_value FROM `wp_postmeta` WHERE `post_id` = %s AND `meta_key` = '_wp_attached_file'",$postid));
echo "<p>Thumb URL: {$get_thumb}</p>";
Again you can always log the SQL string generated by prepare() to ensure it matches your expectations. See https://codex.wordpress.org/Class_Reference/wpdb#Examples for more examples.
I think your syntax has some trouble, so here's your original query revised.
<?php
$get_thumb = $wpdb->get_var(
"
SELECT meta_value
FROM wp_postmeta
WHERE post_id = " . $postid . " AND meta_key = _wp_attached_file
"
);
echo "<p>Thumb URL: {$get_thumb}</p>";
?>
I made sure the PHP is echoing correctly inside the query - I am not sure if that's a problem you were having.
I do recommend the prepared query too. Here is it with what I think is correct syntax (I found it was helpful to prepare items by putting them in a $query_arg_array). See https://codex.wordpress.org/Class_Reference/wpdb.
$postid = get_the_ID();
$query_arg_array = [$postid];
$get_thumb = $wpdb->get_var($wpdb->prepare(
"
SELECT meta_value
FROM wp_postmeta
WHERE post_id = %d AND meta_key = _wp_attached_file
",
$query_arg_array
));
echo "<p>Thumb URL: {$get_thumb}</p>";
If that prepared query didn't work, try putting $postid directly as argument instead of the $query_arg_array.
Remember SQL is super sensitive so even an extra space can throw off your query.
What I'm trying to do is query and sum a custom field in Wordpress for the current logged in user.
I have a website for my writing company that lets writers log in and claim work that they want to do. For each assignment, there is a field called "Word Count" where I input the word count for the article that needs to be done. To calculate how much each writer is owed for an assignment, I use the word count field (with the meta key "assignment_word_count" ) and multiply it by a flat rate that each writer is paid per word.
So far the closest script I've put together that does what I want it do is an SQL script. Unfortunately, it sums ALL of the word count fields instead of just the ones that belong to the current user. If I try to add anything to the code to limit it to the current user, the total drops to zero.
<?php
$now = current_time('mysql');
$sql = "SELECT ";
$sql .= "meta_value FROM $wpdb->posts AS posts, $wpdb->postmeta AS postmeta ";
$sql .= "WHERE posts.ID = postmeta.post_id AND postmeta.meta_key = 'assignment_word_count' ";
$sql .= "AND posts.post_status = 'paid' ";
$sql .= "AND posts.post_date < '$now' ";
$sql .= "AND postmeta.meta_value != '' ";
$results = array(); $values = array();
$results = $wpdb->get_results($sql);
$totalpay = 0;
if (!empty($results)){
foreach ($results as $result) {
$totalpay += $result->meta_value;
}
}
echo 'Total Paid: $' . $totalpay * money_format('0.0040=(#10.2n', $number);
?>
There's also another script I found that is supposed to do exactly what I'm looking for, but it tells me that the implode line is an invalid argument.
<?php
//get current user
global $current_user;
get_currentuserinfo();
// build query of ids by user
$userPosts = get_posts(array('author' => $current_user->ID, 'post_type'=> 'assignments')); //change this
// loop to create array of ids by user
foreach ($userPosts as $post) {
setup_postdata($post);
$ids[] = get_the_ID();
}
$idList = implode(",", $ids); //tun this crap into a list
$meta_key = 'assignment_word_count';//set this to your custom field meta key
$totalpay = $wpdb->get_col($wpdb->prepare("
SELECT meta_value
FROM $wpdb->postmeta
WHERE meta_key = %s
AND post_id in (" . $idList . ")", $meta_key));
echo 'Total pay: $ ' . array_sum( $totalpay); ?>
http://codex.wordpress.org/Function_Reference/wp_get_current_user use this function and get currenet user id
and then
if(currentuserid=="meabox_user")
{
display the metaboxes
.........
}
You should try first to fix your query with a hard coded user id, and then, in a second time, to work on how you pass the current user.
In the query, the right field name is post_author, not author.
I am trying to to filter out numerous (500000) <p>  ;</p> entries in my WordPress site, but I cannot figure out how. I've tried many thing, but still no luck.
Also many \n\n  ; entries should be filtered out and replaced by ''
Here is my code:
$query = mysql_query('select meta_id, meta_value from wp_postmeta where meta_value like "%<p> </p>%" ');
while ($item = mysql_fetch_array($query))
{
echo "item with id:" . $item[meta_id]." found<br>";
$string = preg_replace('~<p> <\/p>~i', '', $item["meta_value"]);
$id = $item[meta_id];
$q = mysql_query('update wp_postmeta set meta_value = "$string" where meta_id = "$id"') or die(mysql_error());
}
Any suggestions?
Why not trying the MySQL replace function?
update table_name set field_name = replace(field_name,'string_to_find','string_to_replace');
I am using the Select query as
SELECT id, ordering FROM `jos_menu` WHERE ordering='".$rec['ordering'] -'1' ."' AND parent = '0'
Here I need all the records whose ordering is less than 1 of the selected record's order($rec['ordering'] = getting from other select query ) when I am trying to echo the query I am not getting complete statement but getting only this -1' AND parent = '0'
here is the whole snippet
$where = ' WHERE (id = ' . implode( ' OR id = ', $cid ) . ')';//Pranav Dave Coded
echo $selquery = "SELECT id, ordering FROM `jos_menu`".$where; //Pranav Dave Coded
$db->setQuery( $selquery );//Pranav Dave Coded
$record = $db->loadAssocList(); //Pranav Dave Coded
if ($model->orderItem($id, -1)) {
echo "<pre>";
print_r($model);
/*exit;*/
//echo $updorderup = mysql_escape_string($model->_db->_sql);//Pranav Dave Coded
foreach($record as $rec)//Pranav Dave Coded
{
echo $aboverow = "SELECT id, ordering FROM `jos_menu` WHERE ordering='".$rec['ordering'] -'1' ."' AND parent = '0'";
$db->setQuery( $aboverow );
$above = $db->loadAssoc();
echo "<pre>";
print_r($above);
}//end of foreach
}//end of if
Please suggest me where I am getting wrong.....
It looks like you may need to unwrap the -1 from the quotes:
WHERE ordering='".($rec['ordering'] - 1)."' AND parent = '0'";
Why do you trying to put everything inline?
Why not to make some preparations first?
Why not to compare resulting query with sample one?
Why don't you check every step if it return proper result?
$val = $rec['ordering'] - 1;
//let's see if $cal has proper value:
echo $val."<br>";
$sql = "SELECT id, ordering FROM `jos_menu` WHERE ordering = $val AND parent = 0";
//let's see if query looks good:
echo $sql;
//let's print sampe query to compare:
echo "<br>" ;
echo "SELECT id, ordering FROM `jos_menu` WHERE ordering = 1 AND parent = 0";
As Daniel said, you need to remove the quotes around the -1. Currently its trying to minus a string, which it wouldn't be happy with at all ;)