Hot answers tagged

3

The meta_query argument (that you can use in a WP_Query) is not enabled by default when using a WC_Order_Query through wc_get_orders() WooCommerce function. But for you can use the undocumented Custom Field Parameters (just like in a WP_Query): meta_key meta_value meta_value_num meta_compare So in your case you can use the following instead: $orders = ...


3

Apparently, WooCommerce is ignoring the meta_query parameter, so that's why you're getting all orders. You can use this code to make the meta_query parameter works with WooCommerce (orders) queries: add_filter( 'woocommerce_get_wp_query_args', function( $wp_query_args, $query_vars ){ if ( isset( $query_vars['meta_query'] ) ) { $meta_query = ...


3

So if i understand you correctly, you want to sort by a value that has to be calculated based on the currency? You can do this, but not within a wordpress wp_query of sorts. Instead you can do one of two things: Method 1: The "do more now, have less hassle later"-way: On saving the currency and price in your save_post action, you calculate the AED-price ...


3

You're missing an s. $query->set('posts_per_page', '1'); However - you probably don't want to do this for every query, since that affects everything - widgets, back end, everything. You should make it conditional. For example: <?php function university_adjust_queries($query) { // If this is the main query, not in wp-admin, and this is the ...


2

You should use prepare only when you're using SQL query - this function takes query and params and returns a safe SQL query filled with given params. Its result is a SQL query. So you can (and should) use it whenever you're creating a SQL query and put some params in it. With $wpdb->delete or $wpdb->update you don't create any string containing SQL ...


2

You have the where clause of the query in the wrong place. Your query should be written like this: $query= "SELECT * FROM files WHERE custom > 0 ORDER BY id DESC LIMIT $from, $site"; As specified here https://dev.mysql.com/doc/refman/8.0/en/select.html in the MySQL reference.


2

First, you don't need a raw SQL query, and by using one you give up all of WP's optimisations. For example, if you run the query twice, it runs twice. If you had used WP_Query to getch those posts though, it would save them the first time to avoid making additional queries. It's even possible those posts are present as a result of other queries. On top of ...


1

Create taxonomy-categories.php or In your archive.php add below code to get post of article and news post type for current category. I assume that your taxonomy for both post type is categories. change categories with your taxonomy and post_type with your post types for news and article from below code. # Article $articleargs = array( 'post_type' ...


1

First, I would recommend making this change in Category.php file, as archive.php is affecting tags as well as other archives according to Wordpress Theme Hierarchy file system. Try adding the two queries like next: $cats = get_the_category(); $cat_id = $cats[0]->term_id; // news $args = array( 'post_type' => 'news', '...


1

You can do this using the WP APIs like this: $args = [ 'post_type' => 'post', 'tax_query' => [ 'relation' => 'AND', [ 'taxonomy' => 'your_taxonomy_name', 'field' => 'slug', 'terms' => array( 'arizona', 'speech-language-pathology' ), ] ], ]; $query = new WP_Query( ...


1

The following function will give you an array with the calculated stock quantity from all product variations for each custom meta field "Brand" value, using WPDB Class (a SQL query): global $wpdb; $results = $wpdb->get_results( " SELECT pm.meta_value as brand, SUM(pm2.meta_value) as stock FROM {$wpdb->prefix}postmeta pm INNER JOIN {$wpdb-&...


1

It's close. There's a few things that stand out. First, I assume you're doing this before the code you shared. It's important to verify a nonce before accepting user input. Read more about Nonce in the Codex. Next, you want to use sanitize_text_field in combination with wp_unslash. Trim is more of a helper function than for sanitizing, but it's helpful ...


1

What you're doing is safe. However, some notes: See the WP_User_Query class in the Codex. This is the, "official," way to do something like this, although you'll get an integer-indexed array of WP_User objects. Instead of the ARRAY_A that you've specified. You probably don't want _my_table, unless your table is called something like wp__my_table (note the ...


1

Your code should look something like this: $this->table_name= 'table_name'; $how_much = $this->wpdb->get_var("SELECT COUNT(*) FROM $this->table_name"); We don't need to use the prepare method, since this is not meant for table names, more here And also... as you what are trying to do is going not in a good direction... I don't what to ...


1

I can't comment due to low reputation so sorry for posting this as an answer, but you are missing $ (dollar sign) near groupcat[0]


1

Generally speaking, you could use the WP Transients API to save the query. // Get any existing copy of our transient data if ( false === ( $special_query_results = get_transient( 'special_query_results' ) ) ) { // It wasn't there, so regenerate the data and save the transient $special_query_results = new WP_Query( 'cat=5&order=random&tag=...


1

The query is right and its working. But should I reset something? Yes! Should I reset them both or just one of them, or is it fine like above? Neither! Your WP_Query object is self contained in $query, so there's no reason to touch the $wp_query global variable, why would you? $wp_query represents the main query, so leave it alone. What you need to ...


1

Firtly, in SQL the % symbol is a "wildcard operator". When used in a LIKE statement, it means "0 or more characters". So LIKE 'Hello%' means any string beginning with Hello, including just Hello. While LIKE '%Hello%' means any string that contains Hello, either at the beginning, middle, or end. $wpdb->prepare() on the other hand uses the % symbol to ...


1

You'd avoid it by creating another menu and menu location. You can do this with a widget area or whatever. Then only call the menu if the page is the home page with is_home() or is_front_page() if it is a page. Here's from twenty seventeen as an example. register_nav_menus( array( 'top' => __( 'Top Menu', 'twentyseventeen' ), '...


1

Follow this flow: Define an empty array $nav_img = array(); outside the loop. Inside the loop while you fetch the featured image, push the URL to the array too like this $nav_img[]='your_image_here' ; Then inside <div class="slider-nav-thumbnails"></div> you can simply do a for each loop to set the images as thumb. UPDATE: Here I added ...


1

As mentioned in the comments you can query the postmeta table directly: public function get_metadata_keys(){ global $wpdb; $meta_query = $wpdb->get_results( " SELECT DISTINCT meta_key FROM {$wpdb->postmeta} " , ARRAY_A ); $meta_keys = wp_list_pluck( $meta_query, 'meta_key' ); return $meta_keys; } ...


1

You have set "roll_number" custom field in tax_query. It should be in "meta_query". Please try the updated code: tion exam_result_filter_function(){ $args = array( 'post_type' => 'exam_result', ); if( isset( $_POST['school_class'] ) || !empty( $_POST['academic_year']) || !empty( $_POST['roll_number']) ){ $args['tax_query'] = ...


1

First insert the global. global $wpdb; Then You have to check your quotes properly. $sql = $wpdb->prepare( "INSERT INTO `$table_name` (`name`,`email`,`phone`,`message`,`rq`,`url`) values ($name, $email, $phone, $address, $msg, 1, $url)"); $wpdb->query($sql); OR $wpdb->insert('$table_name', array( 'name' => $name, '...


1

I'm not sure this will fix it, but maybe try using the current category slug for the $current_category variable, instead of single_cat_title(), like so: $term = get_queried_object(); $current_category = $term->slug; This might help because the category_name parameter in WP_Query, despite its name, should be the term slug. single_cat_title() ...


1

I think I am understanding your correctly...If I'm way off, let me know! // Assuming ideas is an array and you are looking for ideas=>two $query = new WP_Query( array( // You can pass 'any' to post_type, but you get a hold of revisions, attachments, and menu_items. But I am not sure how many Custom Post Types you have ¯\_(ツ)_/¯ '...


1

some_body needs to be run as an action on pre_get_posts. Here's an example of the entire process of adding new query vars. Pay extra attention to myplugin_pre_get_posts: function myplugin_pre_get_posts( $query ) { ... $city = get_query_var( 'city' ); ... } add_action( 'pre_get_posts', 'myplugin_pre_get_posts', 1 );


1

But if you use the same database prefix on both sites, won't theme/plugin changes on the 2nd domain affect the first domain, since both are using the same database? (Theme and plugin settings are usually stored in the wp-options table of the database.) I would set up a separate install of WP, then export from the 1st site to the 2nd site. That will give ...


1

You have misplaced ; in your code, it should be <?php if( empty( $username ) ) { array_push( $errors, "Username is required" ); } ?>


1

But I would like to use my secondary database connection like a standard WordPress query, so that all of the query builder functions are available. Unless the other database is a WordPress database, with the exact same table structure, you can't. The query builder functions in WordPress are designed and built only to work with WordPress' tables. Their ...


1

Thanks! We ran into the same issue and this question helped a lot! Alternate Use Case We're looping through a custom post type called events (surprise, surprise, it stores events). It stores a timestamp in the meta field ev_start (event start time). We actually were able to just use the orderby value meta_value_num (as you can see meta_type is commented out)...


Only top voted, non community-wiki answers of a minimum length are eligible