The query should return thumbnail, title, name, price etc (all the fields)
<?php
query_posts('meta_key=cp_job&meta_value=Sell');
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
the_content();
//
} // end while
} // end if
?>
It should return results where meta_key=cp_job and meta_value=Sell.. I've tried all sorts of queries and this has taken up several hours, as i've yet to find a solution.
I'm working with a theme and the only time i've gotten a result is with this query
$metakey = 'cp_job';
$job = $wpdb->get_col($wpdb->prepare("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = %s ORDER BY meta_value ASC", $metakey) );
foreach ($job as $value) {
echo $value";
}
This query doesn't do what i want to accomplish.
I simply want "SELECT * FROM table WHERE cp_job='Sell'"; but WordPress makes everything so complicated. I don't even know the table which hold my results!...
Any help please..
You may try this:
$args = array(
'post_type' => 'post', // Or custom post type if it's a CPT
'meta_key' => 'cp_job',
'meta_query' => array(
array(
'key' => 'cp_job',
'value' => 'Sell'
)
)
);
Then run the query and loop the $query same way:
$query = new WP_Query( $args );
Related
I have a "usermeta" table in db.
and there are some columns including meta_key and meta_value.
the delivery_location(meta_key) has same meta_value.
so I want to replace current meta_value to new value in deliery_location
(70982, 20, 'delivery_location', 'a:61:{i:0;s:47:"{lat:32.89030473256227, lng:-96.96264851172401}";i:1;s:47:"{lat:32.89359300394015, lng:-96.94936752319336}";i:60;s:0:"";}'),
my question is that how I can replace the all same meta_values at once in mysql?
You can get all users using get_users(). check the below code. code goes in your active theme functions.php file.
function update_all_user_delivery_location(){
$users = get_users( array( 'fields' => array( 'ID' ) ) );
foreach ( $users as $key => $user ) {
$delivery_location = get_post_meta( $user->ID, 'delivery_location', true );
// modify your code here
update_post_meta( $user->ID, 'delivery_location', $delivery_location );
}
}
add_action( 'init', 'update_all_user_delivery_location', 10, 1 );
I need to count how many times my date recorded in the meta key:metakey_AMC_data, in format (d-m-Y) it is contained in the database by comparing it with the current date
$mostra_data_corrente = date('d-m-Y');
$query = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}postmeta
WHERE (meta_key = 'metakey_AMC_data'
AND meta_value = '$mostra_data_corrente')");
$conta_risultati = count($query);
and this I can do perfectly.but now my need is to execute the first query by linking another AND, and specify when the term slug is equal to the category of the event (terms taxonomy), obviously the query is incorrect
SELECT * FROM {$wpdb->prefix}postmeta
WHERE (meta_key = 'metakey_AMC_data'
AND meta_value = '$mostra_data_corrente')
AND(slug = 'aperitivi') "
how can i do this?
You can get that count as well. You need to modify query (code) like follow:
$qry = array(
'post_type' => 'post', // mention your post type to narrow down searching through postmeta table
'meta_query' => array(
array(
'meta_key' => 'metakey_AMC_data',
'meta_value' => $mostra_data_corrente,
'compare' => '='
)
),
'tax_query' => array(
array(
'taxonomy' => 'nameoftaxonomy', // Write the name of taxonomy that you have assinged while you created a CPT (custom post type)
'field' => 'slug',
'terms' => 'aperitivi',
)
)
)
$the_query = WP_Query($qry);
echo $the_query->post_count;
You have to make some necessary changes in above code to suite your requirements. I've added comment where you have to do changes.
I am creating web services for an App, but I am stuck with a insert query. Actually I made a rating system and want to insert value in database with wordpress standard.
Here is my query:
$res = $wpdb->query( $wpdb->prepare(
"INSERT INTO $table_name(rating_postid, rating_posttile, rating_rating, rating_username, rating_userid) VALUES (%d, %s, %d, %s, $d )",
array(
$rating_postid,
$post_title,
$post_rating,
$user_name,
$rating_userid
)
)
);
and here is the other one:
$res = $wpdb->insert(
$table_name,
array(
'rating_postid' => $rating_postid,
'rating_posttile' => $post_title,
'rating_rating' => $post_rating,
'rating_username' => $user_name,
'rating_userid' => $rating_userid
)
);
But no one is working, why?
if($res){
echo 'inserted';
}else{
echo 'not inserted';
}
I am getting else part alwasy
I used these queries very often and they worked me very well, But I am not sure what's wrong with them now... :(
Try this.
$wpdb->insert(
$table_name,
array(
'rating_postid' => $rating_postid,
'rating_posttile' => $post_title,
'rating_rating' => $post_rating,
'rating_username' => $user_name,
'rating_userid' => $rating_userid
),
array(
'%d',
'%s',
'%s',
'%s',
'%d'
)
);
Declaring $wpdb as global and using it to execute an SQL query statement that returns a PHP object
global $wpdb;
$table_name = $wpdb->prefix . "YOUR_TABLE_NAME"; // Enter without prefix
$data = array(
'rating_postid' => $rating_postid,
'rating_posttile' => $post_title,
'rating_rating' => $post_rating,
'rating_username' => $user_name,
'rating_userid' => $rating_userid
);
$result = $wpdb->insert($table_name, $data);
if( $result ){
echo "Inserted..!";
}else{
echo "Something wrong..!";
$wpdb->show_errors();
}
I want to select records from wordpress database based on two taxonomy conditions.
I have the following query.But it lists all the records.I want to filter the following query with taxonomy recipe-category and term 90 .How can i do this?
select DISTINCT p.ID, p.post_author,p.post_date as dte,wt.term_id from
wp_posts p left JOIN wp_postmeta m1 ON p.ID = m1.post_id left JOIN
wp_term_relationships wtr ON (p.ID = wtr.object_id) left JOIN
wp_term_taxonomy wtt ON (wtr.term_taxonomy_id = wtt.term_taxonomy_id)
left JOIN wp_terms wt ON (wt.term_id = wtt.term_id) where
p.post_type='recipe' and p.post_status='publish' AND (wtt.taxonomy =
'recipe-cuisine' and wtt.term_id IN (17) ) order by dte DESC
Why not use WP_QUERY to get posts related to taxonomy (recipe-category) and the term of recipe-category id 90
$taxonomy = 'recipe-category';
$taxonomy_terms = 90;
$args = array(
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'id',
'terms' => $taxonomy_terms,
),
),
);
$posts_related_to_taxonomy = new WP_Query($args);
if( $posts_related_to_taxonomy->have_posts() ) :
echo '<p>';
while($posts_related_to_taxonomy->have_posts()) : $posts_related_to_taxonomy->the_post();
?>
<a href="<?php the_permalink(); ?>">
<span><?php the_title(); ?></span> : <?php echo get_the_excerpt(); ?>
</a>
<br>
<?php endwhile;
echo '</p>';
else :
echo wpautop('No Records found');
endif;
If you want to search between two taxonomy then replace the tax_query argument with the following
'terms' => array( $term) this argument can be use with array of term ids or string , replace taxonomy1 and taxonomy2 with your other taxonomies slug
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'taxonomy1',
'field' => 'id',
'terms' => array( $term)
),
array(
'taxonomy' => 'taxonomy2',
'field' => 'id',
'terms' => array( $term2),
)),
well, the easier method is to use WP's get_posts template function. Looks like parameter category is what you need. AFAIK it allows to use any taxonomy term ID, not only categories.
If you still want to use a direct sql query - it will be easier to get an answer if You'll format the query in question in more readdable way.
I am trying to do a custom sql query to get 2 lots of post_meta keys and there values. Here is my code:
<?php
$queried_post = get_post($post_id);
$title = $term->name ;
$userid = $current_user->user_email;
$result = mysql_query("SELECT * FROM wp_postmeta WHERE meta_key='user_programme_name' AND meta_value='".$title."' AND meta_key='user_email' AND meta_value='".$userid."' ");
$num_rows = mysql_num_rows($result);
if ($num_rows > 0 ) {
?>
<p style="width:430px;">You have already requested a place on this course</p>
<?php } ?>
The first part of the query works when i jsut query the first set of keys and values:
meta_key='user_programme_name' AND meta_value='".$title."'
But as soon as i add the second lot:
AND meta_key='user_email' AND meta_value='".$userid."'
It doesn't work. Is there something I'm doing wrong?
Any help would be greatly appreciated.
Cheers, Dan
Use something like this :
$args = array( 'post_type' => 'myposttype',
'posts_per_page' => …,
'offset' => …,
'meta_query'=>array(
'relation' => 'AND',
array(
'key'=>'_first_key',
'value'=> youvalue,
'type' => 'DATE', // etc.
'compare' => '='
),
array(
'key'=>'_second_key',
'value'=> youvalue,
'type' => 'DATE', // etc.
'compare' => '='
)
)
);
$yourloop = new WP_Query( $args );
Fix your SQL syntax for selecting multiple values on the same column:
<?php
$result = mysql_query("SELECT * FROM wp_postmeta WHERE meta_key IN ('user_programme_name', 'user_email') AND meta_value IN ('.$title.', '.$userid.') ");
?>