mysql query different meta_key and display them separately - mysql

I am a new to mysql and my question is difficult for me to describe. Wordpress stores a lot of postmeta as various meta_keys that contain various meta_values. I'm having a problem trying to query by a particular meta_key but and then being able to display 2 different meta_keys and their values. Here is what I have so far which properly displays all of the video urls from a particular wordpress category...
<?php
$myquery=mysql_query("
SELECT * FROM foy_postmeta AS pm
INNER JOIN foy_term_relationships AS tr ON (pm.post_id = tr.object_id)
INNER JOIN foy_posts AS p ON (pm.post_id = p.ID)
WHERE pm.meta_key='video_url'
AND tr.term_taxonomy_id='27'
AND p.post_status='publish'
ORDER BY p.post_date DESC
");
$rows = array();
while($row=mysql_fetch_array($myquery)){
$rows[] = $row;
}
?>
And then I put this into rows within my html which properly displays all of the video titles and their paths...
<?php
foreach( $rows as $row ) {
<div>my video title: <?php echo $row["post_title"]; ?></div>
<div>my video url: <?php echo $row["meta_value"]; ?></div>
}
?>
Now, what I would like to do is to keep this same row list, but to also write out another meta_key and it's value, something like this....
<?php
foreach( $rows as $row ) {
<div>my video title: <?php echo $row["post_title"]; ?></div>
<div>my video url: <?php echo $row["meta_value"]; ?></div>
<div>my video format is: <?php echo $row["meta_value"]; ?></div>
}
?>
But I'm not sure how I can display 2 meta_values within the same loop since they have separate meta_keys within the same postmeta table.

you can use wp_query for this. Its the wordpress database query for posts which makes things easier when dealing with post_meta.
http://codex.wordpress.org/Class_Reference/WP_Query
for example for the above
$args = array(
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'video_url',
)
),
'tax_query' => array(
array(
'taxonomy' => 'your taxonomy name here',
'field' => 'slug',
'terms' => 'whatever'
)
),
'orderby' => 'date',
'order' => 'DESC',
);
$query= new wp_query($args);
?>
while ( $query->have_posts() ) : $query->the_post();
$meta = get_post_meta( $post->ID );
var_dump($meta);
?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
now for every post you can access the post_meta (check what the array var_dumps for the keys)
or if you want to persist with mysql use a foreach loop for each row and pass the id to get_post_meta($id); you can then use the postmeta for every row.

I found a solution that although works, I'm thinking there is a better way to do it. I removed the specific meta_key item within the query so that I could grab all meta_keys, and then used if statements within the foreach loop to create variables of the particular ones I wanted to display:
<?php
$myquery=mysql_query("
SELECT * FROM foy_postmeta AS pm
INNER JOIN foy_term_relationships AS tr ON (pm.post_id = tr.object_id)
INNER JOIN foy_posts AS p ON (pm.post_id = p.ID)
WHERE tr.term_taxonomy_id='27'
AND p.post_status='publish'
ORDER BY p.post_date DESC
");
$rows = array();
while($row=mysql_fetch_array($myquery)){
$rows[] = $row;
}
<?php
foreach( $rows as $row ) {
// sets video url and video layout values as variables
if ($row["meta_key"] == "video_url" ){ $videourl = $row["meta_value"]; }
if ($row["meta_key"] == "video_layout" ){ $videolayout = $row["meta_value"]; }
if ($row["meta_key"] == "video_url" ){
?>
<div>my video url: <?php echo $videourl ?></div>
<div>my video format is: <?php echo $videolayout ?></div>
<?php
}
}
?>

Related

How to parse php inside html

I tried to parse the php query into html, but on the html page it shows array.
My query looks like this: $comments = get_comments( array('post_id' => $post->ID, 'status' => 'approve') );
I tried to print it on the html page using this: $html .= $comments;
As you can see from the examples.
And as Tom J Nowell commented, you need to loop through each comment and append the comment to the $html.
$args = array(
'status' => 'approve',
'post_id' => $post->ID,
);
$comments = get_comments( $args );
foreach ( $comments as $comment ) :
$html .= $comment->comment_author . '<br />' . $comment->comment_content . '<br />';
endforeach;

Query posts custom field: check list inside a ACF repeater

I'm trying to query all posts that have a specific check box selected inside a repeater field.
I found this tutorial and what I want to do is a mix of topic 3 and 4, but I couldn't accomplish by my self, my first attempt was to query all posts and print just one checked item:
<?php
$args = array(
'post_type' => 'modalidade',
'posts_per_page' => -1,
);
$loop = new WP_Query( $args );
?>
<?php
// check if the repeater field has rows of data
if( have_rows('horarios') ):
// loop through the rows of data
while ( have_rows('horarios') ) : the_row();
$dia_da_semana = get_sub_field_object( 'dia_da_semana' );
$horario = get_sub_field_object( 'horario' );
if ( in_array(1, $dia_da_semana['value'])) {
echo 'segunda';
print_r( $horario['value']);
echo '<br>';
}
endwhile;
endif;
?>
But don't think that is a good solution. Maybe there is a better way to achieve what I'm trying to do.
I used the tutorial found here and tweked my code and my data structure a little bit and made it. The final code looks like this:
// filter
function my_posts_where( $where ) {
$where = str_replace("meta_key = 'time_%", "meta_key LIKE 'time_%", $where);
return $where;
}
add_filter('posts_where', 'my_posts_where');
// args
$args = array(
'posts_per_page' => -1,
'post_type' => 'class',
'meta_query' => array(
array(
'key' => 'time_%_day_of_week',
'value' => 'monday',
'compare' => 'LIKE'
),
)
);
One change that was very important to me was to replace the 'numberposts' => -1, to 'posts_per_page' => -1, to get all the posts. The property numberposts wasn't working.

Move Wordpress Custom Post Type Content to a Custom Field Value

Hey all I have a 163 posts imported into a custom post type from a csv file, it's all great except one little problem. The plugin forced me to put a column into the content field - so I chose one that I was going to put as a custom field - is there anyway for me to now convert all of them into a custom field and clear the content area?
Thanks!
You could put something like this (untested) in functions.php
add_action( 'get_header', 'so19967768_update_posts' );
function so19967768_update_posts()
{
if( ! isset( $_GET['update'] ) )
return;
$posts = get_posts( array( 'post_type' => 'my_post_type', 'posts_per_page' => -1 ) );
foreach( $posts as $post )
{
setup_postdata( $post );
$value = $post->post_content;
update_post_meta( $post->ID, 'my_meta_key', $value );
wp_update_post( array(
'ID' => $post->ID,
'post_content' => ''
) );
}
echo 'update complete!';
die();
}
and visit yoursite.com/?update=1 to run the update

Drupal querying database table

I am a newbie in Drupal, I have a table in a drupal database. I wanted to query all the content of it and display in a tabular format. Whenever I execute the query the link directs me to the blank page and nothing appears. I have attached the PhP code for the reference. Any help will be highly appreciated.
<?php
$header = array('Name', 'Age', 'Sex','University');
$rows = array();
$sql = 'SELECT Name, Age, Sex,University FROM {data_pulling} ORDER BY Name';
$res = db_query($sql);
while ($row = db_fetch_array($res)) {
$rows[] = $row;
}
print theme('table', $header, $rows);
?>
At a guess you're using Drupal 7 but are trying to use API functions from Drupal 6.
Try this
foreach ($res as $row) {
$rows[] = (array) $row;
}
print theme('table', array('rows' => $rows, 'header' => $header));
Have a look at the docs for db_query and theme_table for more information

How to get all metavalue for a specific custom field for all posts in one category?

I'm trying to get the sum for all the meta values of one custom field. I now get the sum for the metavalues of the whole site. I want it to be restricted to the category you're in.
The code now looks like this:
<?php
$thevariable = $wpdb->get_var($wpdb->prepare("
SELECT SUM($wpdb->postmeta.meta_value)
FROM $wpdb->postmeta, $wpdb->posts
WHERE $wpdb->postmeta.meta_key='mycustomfield'
AND $wpdb->postmeta.post_id=$wpdb->posts.id
"));
echo '<h1>' . $thevariable . '</h1>';
?>
Can somebody help me to filter out one category?
Would be amazing!
M
Inside your WP files, you can add this lines; I guess It will do what you are looking for.
<?php
$MySum = 0;
$args = array(
'category_name' => 'MyCategory',
'meta_key' => 'mycustomfield',
'posts_per_page' => '-1' );
// The Query
$the_query = new WP_Query( $args);
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
$MySum += get_post_meta($post_id, $key, true);
endwhile;
echo '<h1>' . $MySum . '</h1>';
// Reset Post Data
wp_reset_postdata();
?>