Freebase MQL query issues - freebase

below is my query code.. I am trying to put together several types to get one query to build a set of data off of...but the query does not work from php, it does work inside of query editor on freebase.com
<?php
function getQuery($type,$id=null){
//leaves off closing so you can return and append to the end of everything following...
$query = "[{
\"type\":\"$type\",
\"limit\":10,
\"id\":null,
\"name\":null,";
return $query;
}
function getSportsTeamsStructure(){
$query = "\"/sports/sports_team/sport\":null,";
$query .="\"/sports/sports_team/league\":[{\"team\":null,\"league\":null,\"from\":null,\"to\":null}],";
$query .="\"/sports/sports_team/location\":[{".getLocationStructure()."}],";
$query .="\"/sports/sports_team/founded\":null,";
$query .="\"/sports/sports_team/arena_stadium\":[{".getArchitectureStructure()."}],";
$query .="\"/sports/sports_team/colors\":[{}],";
$query .="\"/sports/sports_team/team_mascot\":[{}],";
$query .="\"/sports/sports_team/championships\":[{}],";
$query .="\"/sports/sports_team/fight_song\":[{}],";
$query .="\"/sports/sports_team/roster\":[{\"position\":[{}],\"team\":[{}],\"number\":[],\"from\":null,\"to\":null}],";
$query .="\"/sports/sports_team/coaches\":[{\"position\":[{}],\"from\":null,\"to\":null,\"team\":[{}],\"coach\":[{}]}]";
return $query;
}
function getPeoplePersonStructure(){
$query = '';
$query .= "\"/people/person/date_of_birth\":null,";
$query .="\"/people/person/place_of_birth\":null,";
$query .="\"/people/person/nationality\":null,";
$query .="\"/people/person/gender\":null,";
$query .="\"/people/person/profession\":[{}],";
$query .="\"/people/person/religion\":[{}],";
$query .="\"/people/person/ethnicity\":[{}],";
$query .="\"/people/person/parents\":[{}],";
$query .="\"/people/person/children\":[{}],";
$query .="\"/people/person/sibling_s\":[{}],";
$query .="\"/people/person/spouse_s\":[{}],";
$query .="\"/people/person/height_meters\":null,";
$query .="\"/people/person/weight_kg\":null,";
$query .="\"/people/person/education\":[{}],";
$query .="\"/people/person/age\":[{}],";
$query .="\"/people/person/notable_professions\":[{}],";
$query .="\"/people/person/languages\":[{}]";
return $query;
}
function getCommonTopicStructure(){
$query = '';
$query .="\"/common/topic/image\":[{}],";
$query .="\"/common/topic/article\":[{}],";
$query .="\"/common/topic/notable_for\":[],";
$query .="\"/common/topic/notable_types\":[],";
$query .="\"/common/topic/alias\":[],";
$query .="\"/common/topic/description\":[],";
$query .="\"/common/topic/official_website\":[],";
$query .="\"/common/topic/topic_equivalent_webpage\":[],";
$query .="\"/common/topic/subjects\":[],";
$query .="\"/common/topic/subject_of\":[],";
$query .="\"/common/topic/weblink\":[],";
$query .="\"/common/topic/social_media_presence\":[{}]";
return $query;
}
function getArchitectureStructure(){
$query = '';
$query = "\"/architecture/structure/architect\":[{".getPeoplePersonStructure()."}],";
$query = "\"/architecture/structure/architectural_style\":[],";
$query .="\"/architecture/structure/owner\":[{}],";
$query .="\"/architecture/structure/architecture_firm\":[{}],";
$query .="\"/architecture/structure/height_meters\":null,";
$query .="\"/architecture/structure/construction_cost\":null,";
$query .="\"/architecture/structure/destruction_date\":null,";
$query .="\"/architecture/structure/destroyed_by\":null,";
$query = "\"/architecture/structure/address\":[{".getMailingAddressStructure()."}],";
$query .="\"/architecture/venue/capacity\":null";
return $query;
}
function getLocationStructure(){
$query = '';
$query .= "\"/location/location/geolocation\":[{\"id\":null,\"longitude\":null,\"latitude\":null,\"daturn\":null,\"elevation\":null}],";
$query .= "\"/location/location/contains\":[{}],";
$query .="\"/location/location/containedby\":[{}],";
$query .="\"/location/location/adjoin_s\":[{}],";
$query .= "\"/location/location/area\":null,";
$query .="\"/location/location/time_zones\":[{}],";
$query .= "\"/location/location/people_born_here\":[{}],";
$query .= "\"/location/location/events\":[{}],";
$query .="\"/location/location/nearby_airports\":[{}],";
$query .="\"/location/location/near\":[{}],";
$query .="\"/location/location/street_address\":{{".getMailingAddressStructure()."}],";
return $query;
}
function getMailingAddressStructure(){
$query = '';
$query = "\"street_address\":null,\"street_address_2\":null,\"citytown\":null,\"state_province_region\":null,\"postal_code\":null,\"country\":null";
return $query;
}
?>

Related

Updating ACF field with purchased product's term ids - WooCommerce

I am trying to update the a 'product_themes' user field in WooCommerce/WP with a function that collects the term_ids for the items in an order and adds them to the user's profile under the 'product_themes' field at the time when the order is placed. This is working when I specify an array of IDs in the code but does not seem to be collecting the term IDs from each item for a custom 'theme' taonomy.
add_action('woocommerce_thankyou', 'add_theme_to_user');
add_action('woocommerce_new_order', 'add_theme_to_user');
function add_theme_to_user($order_id){
$current_user = wp_get_current_user();
$userID = $current_user->ID;
$user = 'user_' . $userID;
$prodCats = array();
$order = new WC_Order( $order_id );
$items = $order->get_items();
$push = array();
foreach ( $items as $item ) {
$item_id = $item['order_item_id'];
$product_name = $item['name'];
$terms = wp_get_post_terms( $itemID, 'theme' );
foreach ( $terms as $term){
array_push($push, $term->term_id);
}
}
update_field('product_themes', $push, 'user_' . $userID);
}
I have consulted the WooCommerce documentation and searched for a solution on any similar problems but I cannot seem to get this to work. Am I missing something?
Many thanks.
I found the solution myself, changed $itemID = $item['order_item_id']; to $itemID = $item['product_id'];
add_action('woocommerce_thankyou', 'add_theme_to_user');
add_action('woocommerce_new_order', 'add_theme_to_user');
function add_theme_to_user($order_id){
$current_user = wp_get_current_user();
$userID = $current_user->ID;
$user = 'user_' . $userID;
$prodCats = array();
$order = new WC_Order( $order_id );
$items = $order->get_items();
$push = array();
foreach ( $items as $item ) {
$itemID = $item['product_id'];
$terms = wp_get_post_terms( $itemID, 'theme' );
foreach ( $terms as $term){
array_push($push, $term->term_id);
update_field('product_themes', $push, 'user_' . $userID);
}
}
update_field('product_themes', $push, 'user_' . $userID);
}

Avoid looking into Post content while searching

I have created a query using WP_Query for searching posts. Below is the query
$args = array(
's' => 'Keyword'
);
$the_query = new WP_Query( $args );
But the argument 's' always looks into post_title and post_content. I want to change it so it look into post_title only and not into post_content. How to do that?
At first you need to modify the search function of wp, using posts_search filter hook Check this Link, then run your query and argument.
After that, you need to remove that hook after use.
The example code is bellow
<?php
function wpse_11826_search_by_title( $search, $wp_query ) {
if ( ! empty( $search ) && ! empty( $wp_query->query_vars['search_terms'] ) ) {
global $wpdb;
$q = $wp_query->query_vars;
$n = ! empty( $q['exact'] ) ? '' : '%';
$search = array();
foreach ( ( array ) $q['search_terms'] as $term )
$search[] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $n . $wpdb->esc_like( $term ) . $n );
if ( ! is_user_logged_in() )
$search[] = "$wpdb->posts.post_password = ''";
$search = ' AND ' . implode( ' AND ', $search );
}
return $search;
}
add_filter( 'posts_search', 'wpse_11826_search_by_title', 10, 2 );
$args = array(
's' => 'Keyword'
);
$the_query = new WP_Query( $args );
remove_filter( 'posts_search', 'wpse_11826_search_by_title', 500 );
//Then your code goes here....
?>

Wordpress filter post before displaying

Currently all posts is being listed by category,
i need to filter the list by a meta-key. I used the filter('posts_where') but all the query has been changed.
I need to add the WHERE condition in the existing sql that is being generated.
function get_filtered_post( $args, $meta, $value ){
$posts = get_posts( $args );
$ids = array();
foreach( $posts as $post ){
$id = $post->ID;
if( get_post_meta( $id, $meta, true ) == $value ){
$ids[] = $id;
}
}
return $ids;
}
$args = array( 'post_type' => 'post', 'posts_per_page' => -1 );
$IDofPost = get_filtered_post( $args, 'my-meta-key', 'the-metas-value' );
foreach( $IDofPost as $id ){
echo get_the_title( $id );
}
Hope this function will help. It returns post id with the given post meta values.

Get prices only on specific category for Woocommerce

I am trying to display prices next to variations on Woocommerce, which I could do just fine. Now instead of showing prices on ALL variations I just want to show prices next to variations belonging to a specific category.
This is what I have come up with so far and logically it makes sense, but not working on this particular category. Any suggestions on how to troubleshoot?
//get prices of variations
function display_price_in_variation_option_name( $term ) {
global $wpdb, $product;
$result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );
$term_slug = ( !empty( $result ) ) ? $result[0] : $term;
$query = "SELECT postmeta.post_id AS product_id
FROM {$wpdb->prefix}postmeta AS postmeta
LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
WHERE postmeta.meta_key LIKE 'attribute_%'
AND postmeta.meta_value = '$term_slug'
AND products.post_parent = $product->id";
$variation_id = $wpdb->get_col( $query );
$parent = wp_get_post_parent_id( $variation_id[0] );
if ( $parent > 0 ) {
$_product = new WC_Product_Variation( $variation_id[0] );
$itemPrice = strip_tags (woocommerce_price( $_product->get_price() ));
//this is where you can actually customize how the price is displayed
return $term . ' (' . $itemPrice . ')';
}
return $term;
//Add prices to variations
global $post;
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $termItem ) $categories[] = $termItem->slug;
if ( in_array( 'custom-pet-portrait-painting', $categories ) ) {
add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );
} //end function
}
Or can I use get_terms to wrap the below code in which shows the variations price? I pretty much just need to show the prices for variations pertaining to one category.
if (has_term ('my-category', 'product_cat')) {
//get prices of variations
function display_price_in_variation_option_name( $term ) {
global $wpdb, $product;
$result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );
$term_slug = ( !empty( $result ) ) ? $result[0] : $term;
$query = "SELECT postmeta.post_id AS product_id
FROM {$wpdb->prefix}postmeta AS postmeta
LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
WHERE postmeta.meta_key LIKE 'attribute_%'
AND postmeta.meta_value = '$term_slug'
AND products.post_parent = $product->id";
$variation_id = $wpdb->get_col( $query );
$parent = wp_get_post_parent_id( $variation_id[0] );
if ( $parent > 0 ) {
$_product = new WC_Product_Variation( $variation_id[0] );
$itemPrice = strip_tags (woocommerce_price( $_product->get_price() ));
//this is where you can actually customize how the price is displayed
return $term . ' (' . $itemPrice . ')';
}
return $term;
add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );
} //end has_term

ordering wordpress post_meta - lowest to highest

I am currently looping through all posts and displaying a post_meta value like this:
global $wpdb;
$table = $wpdb->prefix . 'postmeta';
$theid = get_the_id();
$getLowestPrice = $wpdb->get_results("SELECT * FROM $table WHERE meta_value = '$theid'");
foreach ( $getLowestPrice as $post ){
get_post_meta( $post->post_id, '_wholesale_price', false );
}
Is there a way to order the results, lowest -> highest? At the moment they are getting displayed randomly, or as they were entered.
use the following code
<?php
global $wpdb;
$table = $wpdb->prefix . 'postmeta';
$theid = get_the_id();
$getLowestPrice = $wpdb->get_results("SELECT * FROM $table WHERE meta_value = '$theid'");
$all_post = array();
foreach ( $getLowestPrice as $post ){
$all_post[] = $post->post_id;
}
$query = new WP_Query( array( 'post__in' => $all_post, 'orderby' => 'meta_value', 'meta_key' => '_wholesale_price','order' => 'ASC') );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo '<div>' . get_post_meta( $get_the_ID(), '_wholesale_price', false );() . '</div>';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>`
This looks a bit wrong to me. If you're trying to order posts by the value of a meta key (unless I'm mistaken, that is what you're doing) then I would normally go about it using WP_Query()
global $wp_query;
$args = array(
'post_type' => '[YOUR POST TYPE]',
'meta_key' => '_wholesale_price',
'orderby' => 'meta_value meta_value_num', // meta_value_num for value
'order' => 'ASC' // or DSC for low/high
);
$wp_query - new WP_Query( $args );
if ( $wp_query->have_posts() ) {
while ( $wp_query->have_posts() ) {
$wp_query->the_post();
// Your loop
}
}

Resources