WP_Query does not filter results - php

Spent 3 days trying to get the WP_Query to work with Elementor, however had no luck so far. Elementor either refuses to process the query or does not filter the posts at all. Can somebody help me and point out some mistakes that I have in my code?
Context: the query looks for current WP user's enrollment records of post_type 'tutor_enrolled' in 'wpv1_posts' and collects IDs of user's enrolled courses into $course_ids[]. The query should then filter only the posts with IDs matching the $course_ids array, therefore displaying all of current user's courses on Elementor frontend.
function custom_query_callback( $query ) {
$current_user = wp_get_current_user();
$args = array(
'post_type' => 'tutor_enrolled',
'author' => $current_user->ID,
);
$enrollments = new WP_Query( $args );
if ( $enrollments->have_posts() ) {
$course_ids = array();
while ( $enrollments->have_posts() ) {
$enrollments->the_post();
$course_ids[] = get_post_field( 'post_parent' );
}
wp_reset_postdata();
$args = array(
'post_type' => 'courses',
'post__in' => $course_ids,
'posts_per_page' => -1,
'orderby' => 'post__in'
);
$query = new WP_Query ( $args );
}
}
}
add_action( 'elementor/query/custom_query_callback', 'custom_query_callback' );
I tried calling get_course_ids() and custom_query_callback() as two separate functions and trying to pass the $course_ids array in $query->set( 'post__in', $course_ids );.
Then I also moved the variables outside of the function, ending up with:
$current_user = wp_get_current_user();
$args = array(
'post_type' => 'tutor_enrolled',
'author' => $current_user->ID,
);
$enrollments = new WP_Query( $args );
if ( $enrollments->have_posts() ) {
$course_ids = array();
while ( $enrollments->have_posts() ) {
$enrollments->the_post();
$course_ids[] = get_post_field( 'post_parent' );
}
wp_reset_postdata();
}
add_action( 'elementor/query/custom_query_callback', function( $query ) {
$query->set( 'post__in', $course_ids );
});
None of the above resolved my issue and kept causing errors in Elementor.

Related

wp_set_object_terms not setting the terms for the post

This code does not set the object terms. Please help me sort out the issue. The code results in the posts list and doesn't set the terms.
I have checked the below data.
The query results in the list of post.
calculation part in the loop results in the value (lesser or greater than 0)
function set_expired_job_categories() {
global $post;
$current_time = time();
$taxonomy = 'current-status';
$job_expired_id = 368;
$job_ongoing_id = 367;
// Set our query arguments
$args = array(
'fields' => 'ids', // Only get post ID's to improve performance
'post_type' => 'job', // Post type
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
'taxonomy' => 'current-status',
'field' => 'slug',
'terms' => array( 'ongoing' ),
),
);
$job_expiration_query = new WP_Query( $args );
// Check if we have posts to delete, if not, return false
if( $job_expiration_query->have_posts() ){
while( $job_expiration_query->have_posts() ){
$job_expiration_query->the_post();
$postid = get_the_ID();
$expire_timestamp = rwmb_meta( 'deadline_date' );
if ( $expire_timestamp ) {
$seconds_between = ( (int)$expire_timestamp - (int)$current_time );
if ( $seconds_between <= 0 ) {
wp_set_object_terms( $postid, (int)$job_expired_id, $taxonomy, true );
wp_remove_object_terms( $postid, (int)$job_ongoing_id, $taxonomy );
}
}
}wp_reset_postdata();
}
}
add_action( 'set_job_categories', 'set_expired_job_categories', 20, 2 );
As it is in the functions file, I need to set the global $wp_taxonomies to populate the taxonomies data initially. Also, instead of using the tag_ID, I have revised with the slug. These two changes helped to work out the code. The revised code is below for reference.
Thank you all for your efforts.
function set_expired_job_categories() {
global $post;
global $wp_taxonomies;
$current_time = time();
$taxonomy = 'current-status';
$job_expired_id = 'expired';
$job_ongoing_id = 'ongoing';
// Set our query arguments
$args = array(
'fields' => 'ids', // Only get post ID's to improve performance
'post_type' => 'job', // Post type
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
'taxonomy' => 'current-status',
'field' => 'slug',
'terms' => array( 'ongoing' ),
),
);
$job_expiration_query = new WP_Query( $args );
// Check if we have posts to set categories, if not, return false
if( $job_expiration_query->have_posts() ){
while( $job_expiration_query->have_posts() ){
$job_expiration_query->the_post();
$postid = get_the_ID();
$expire_timestamp = rwmb_meta( 'deadline_date' );
if ( $expire_timestamp ) {
$seconds_between = ( (int)$expire_timestamp - (int)$current_time );
if ( $seconds_between >= 0 ) {
}else {
wp_set_object_terms( $postid, (int)$job_expired_id, $taxonomy, true );
wp_remove_object_terms( $postid, (int)$job_ongoing_id, $taxonomy );
}
}
}
wp_reset_postdata();
}
}
// hook it to low priority value, due to CPT and Taxonomies
add_action( 'set_job_categories', 'set_expired_job_categories', 20, 2 );
Reference: https://wordpress.org/support/topic/wp_set_object_terms-in-loop-is-not-work-in-taxonomy-cpt/

WordPress Find ID of a Post with a specific meta value

I have about 25,000 posts here (and rising). All of them under a Custom Post Type "lead".
Each post has meta information, including a variable called "uniqid".
this uniqid is a url parameter. now i need the post id where exactly this uniqid exists.
Now my question is if there is a way to speed up this determination.
I have tried 2 ways once a wp_query and get_results.
I ask because this function that determines the post id is always noted by plugin "query monitor" that it is slow.
These are the 2 approaches, both work. I just wonder if it is possible to speed up here?
The WP_Query solution.
$post_id = false;
$args = array(
'posts_per_page' => -1,
'post_type' => 'lead',
'fields' => 'ids',
'orderby' => 'date',
'order' => 'ASC',
'post_status' => 'publish',
'meta_key' => 'uniqid',
'meta_value' => $uniqid
);
$query = new WP_Query( $args );
if( $query->have_posts() ) {
while( $query->have_posts() ) {
$query->the_post();
// Get Post ID for return
$post_id = get_the_id();
// Break loop, when matching id was found
$uniqidGPM = get_post_meta( $post_id, 'uniqid' , true );
if($uniqidGPM == $uniqid) {
$post_found = true;
break;
}
}
}
wp_reset_postdata();
return $post_id;
The select get_results solution.
$post_id = false;
global $wpdb;
$selectQuery = "SELECT wp_wkdm_posts.ID FROM wp_wkdm_posts INNER JOIN wp_wkdm_postmeta ON ( wp_wkdm_posts.ID = wp_wkdm_postmeta.post_id ) WHERE 1=1 AND ( ( wp_wkdm_postmeta.meta_key = 'uniqid' AND wp_wkdm_postmeta.meta_value = '".$uniqid."' )) AND wp_wkdm_posts.post_type = 'lead' AND ((wp_wkdm_posts.post_status = 'publish')) GROUP BY wp_wkdm_posts.ID ORDER BY wp_wkdm_posts.post_date ASC";
$selectQueryResult = $wpdb->get_results($selectQuery);
if (empty($selectQueryResult)) {
return $post_id;
}else{
$post_id = $selectQueryResult[0]->ID;
return $post_id;
};
Please use this meta_query condition on the same query, it helps you.
$args = array(
'post_type' => 'lead',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
'key' => 'uniqid',
'value' => 'YOUR_VALUE'
)
);
$query = new WP_Query($args);

Wordpress: Show three random posts on every page refresh

In my WordPress, I am able to show one random post by using the below code:
global $post;
if ( post_type_exists( 'testimonial' ) ) {
$testimonial_query = new WP_Query( array(
'post_type' => 'testimonial',
'orderby' => 'rand',
'posts_per_page' => -1
) );
if ( $testimonial_query->have_posts() ) {
$random_int = rand( 0, $testimonial_query->post_count - 1 );
$post = $testimonial_query->posts[$random_int];
setup_postdata( $post );
// do something with post - e.g. the_excerpt(), the_content(), etc.
}
// Restore original post data
wp_reset_postdata();
}
Ref: https://barn2.co.uk/how-to-display-a-random-post-in-wordpress/
Help me to show 3 random posts by using the above method.
Can you try using in array property or WP_Query. A sample code is given belwo
$results = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE `post_type` = 'testimonial' ORDER BY RAND() LIMIT 3" );
$ids = array();
foreach($results as $$result){
$ids[] = $results->ID;
}
$args = array(
'post_type' => array( 'testimonial' ),
'orderby' => 'ASC',
'post__in' => $ids
);
$testimonial_query = new WP_Query( $args );

How to update values of a specific custom field for every woocommerce product at once

I want to update a value to a specific custom field in all Woocommerce products at once.
But this is not working
function update_products_by_x(){
$products_ids = get_posts( array(
'post_type' => 'product', // or ['product','product_variation'],
'post_status' => 'publish',
'fields' => 'ids'
) );
foreach ( $products_ids as $product_id ) {
$product = wc_get_product($product_id);
update_post_meta($product_id, 'field_name_here', esc_attr('field_value_here'));
}
}
add_action('wp_footer', 'update_products_by_x');
Use WP_Query to retrieve all products and hook your function to init:
add_action('init', 'update_products_by_x');
function update_products_by_x(){
if(isset($_GET['update_woo_products'])){
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
update_post_meta($product->ID, 'field_name_here', esc_attr('field_value_here'));
endwhile;
wp_reset_query();
die('done');
}
}
If you notice there is a small condition to make sure that this only run if requested, so to run this function you'd need to visit the website and make sure you add the parameter update_woo_products as a query string (eg: https://example.com/?update_woo_products=1 )
This will run the function and show a white page with the message done, which means that the condition was met and the function was executed successfully.

Wordpress woocommerce multiple orderby args loop

Does anyone know how to incorporate a while loop into a basic order by filter? I have the following basic filter in my functions.php page that orders my products by the featured products and it works:
add_filter('woocommerce_get_catalog_ordering_args', 'am_woocommerce_catalog_orderby');
function am_woocommerce_catalog_orderby( $args ) {
if(!$_GET['orderby']) {
$args = array(
'orderby' => array( 'meta_value' => 'DESC' ),
'meta_key' => '_featured'
);
return $args;
}
}
My problem is I want to use some more advanced while loops to try and get multiple different arguments tied into the same args. I am trying to show my featured products first, then load that into an array then get my next loop and only show the products that are 6 months old or younger and add that to my array, then show everything else and sort them by menu_order and title and add it to my array as shown below:
add_filter('woocommerce_get_catalog_ordering_args', 'am_woocommerce_catalog_orderby');
function am_woocommerce_catalog_orderby( $my_post_array ) {
if(!$_GET['orderby']) {
//First loop - show featured
$args['orderby'] = array( 'meta_value' => 'DESC' );
$args['meta_key'] = '_featured';
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$post_id = get_the_ID();
$my_post = my_post_function($post_id);
//Store the items in an array
$my_post_array [] = $my_post;
query_posts($args);
endwhile;
wp_reset_query();
//Second loop - Show newer than 6 months
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'date_query' => array(
array(
'before' => '6 months ago',
),
)
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$post_id = get_the_ID();
$my_post = my_post_function($post_id);
//Store the items in an array
$my_post_array [] = $my_post;
query_posts($args);
endwhile;
wp_reset_query();
//Third loop - show everything else and sort by menu_order and title
$args['orderby'] = array( 'menu_order' => 'ASC', 'title' => 'ASC' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$post_id = get_the_ID();
$my_post = my_post_function($post_id);
//Store the items in an array
$my_post_array [] = $my_post;
query_posts($args);
endwhile;
wp_reset_query();
//Remove duplicate entries from the array
array_unique ( $my_post_array, SORT_STRING );
}
}
Then the last thing I do is remove my duplicates. Only problem is it does not work and I am not sure where I am going wrong, I have not done something like this before so I could be working in the wrong area or missing something obvious. Anyone have ideas on how I can trouble shoot this or does anyone see something that may be causing me issues? Thanks for the help, still learning and trying to improve!

Categories