Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

I have a wordpress site which I am selling "courses" and manage "payme WordPress

  • REFUNDED

I have a wordpress site which I am selling "courses" and manage "payments" for those courses.
I have 2 custom post types for that matter: "course" (for displaying the courses)
and "Course Payment" for holding the payment for each course

in my custom post type called: course_payment
In my admin I have created couple of columns
One of them pulls another Array of post types ("course") which have a connection
to my post type course_payment
See image here:
https://imgur.com/QlQfYns

I would like to be able to add filter above to sort"course_payment" by "course"
I managed to create 2 functions:

1) for adding the select drop down above
2) for the filter action

1) is working but the filter itself is not working
it redirects me the "results" page but the results are empty

here is my code:

function lam_filter_courses($post_type, $which) {
if ($post_type === 'course_payment') {
$selected = $_GET['course'] ? $_GET['course'] : '';
echo '<select name="course" style="float:none;margin-right:15px;">';
echo '<option value="">' . __( 'Filter by Course', 'lam' ) . '...</option>';
$courses = get_posts( [ 'post_type' => 'course', 'numberposts' => - 1 ] );
foreach ( $courses as $o ) {
$selected = (int) $o->ID === (int) $selected ? ' selected="selected"' : '';
echo '<option value="' . $o->ID . '"' . $selected . '>' . $o->post_title . '</option>';
}
echo '</select>';
}
}

add_action('restrict_manage_posts', 'lam_filter_courses', 10, 2);


function filter_payments_by_course( $query ) {
global $typenow;

if ( $typenow === 'course_payment' && is_admin() ) {
$course = $_GET['course'] ? $_GET['course'] : null;
if ( $course ) {
$meta_query = [
[
'key' => 'course_id',
'value' => (int)$course,
],
];
$query->set( 'meta_key', 'course_id' );
$query->set( 'meta_query', $meta_query );
}
}
}
add_action( 'pre_get_posts', 'filter_payments_by_course', 1 );


Second action filter_payments_by_course is not working
I need to be able to filter "Course Payment" by "Courses"

Answers (4)

2021-02-15

timDesain Nanang answers:

here is the second function:

function filter_payments_by_course( $query ) {
global $typenow;

if ( $typenow === 'course_payment' && is_admin() ) {
$course = isset($_GET['course']) ? intval($_GET['course']) : 0;
if ( $course>0 ) {
$query->query_vars['meta_key'] = 'course_id';
$query->query_vars['meta_value'] = $course;
}
}
}
add_filter( 'parse_query', 'filter_payments_by_course');


hamergil comments:

Hi
It's not working
here is the URL that I'm getting
http://localhost/mysite/wp-admin/edit.php?s&post_status=all&post_type=course_payment&action=-1&m=0&course=519&filter_action=%D7%A1%D7%99%D7%A0%D7%95%D7%9F&paged=1&action2=-1

And no results (attached image)
maybe also check the first function?


hamergil comments:

Hi
It's not working
here is the URL that I'm getting
http://localhost/mysite/wp-admin/edit.php?s&post_status=all&post_type=course_payment&action=-1&m=0&course=519&filter_action=%D7%A1%D7%99%D7%A0%D7%95%D7%9F&paged=1&action2=-1

And no results - https://imgur.com/NHVttE4
maybe also check the first function?

2021-02-15

Hariprasad Vijayan answers:

Try with parse_query instead of pre_get_posts.

function filter_payments_by_course($query)
{
global $typenow;

if ($typenow === 'course_payment' && is_admin()) {
$course = $_GET['course'] ? $_GET['course'] : '';
if (!empty($course)) {
$query->query_vars = array(array(
'field' => 'course_id',
'value' => $course,
'compare' => '=',
'type' => 'CHAR'
));

}
}
return $query;
}
add_filter( 'parse_query', 'filter_payments_by_course' , 10);


hamergil comments:

Same as asnwer above
it is giving me "Not found" results
with this URL trail
edit.php?s&post_status=all&post_type=course_payment&action=-1&m=0&course=519&filter_action=filter&paged=1&action2=-1

2021-02-15

Bob answers:

First of all how both custom post types are linked?

do you use meta field to link each other?
what is meta field name?

can you show the code how column of course is created in course_payment custom post type?
It will have clue.


hamergil comments:

1) I'm pretty sure the meta field is $course_id
2) Here is the code
hope that helps

function course_payment_columns( $columns ) {
$columns = [
'cb' => '<input type="checkbox" />',
'payment_date' => __( 'Payment date' ),
'course' => __( 'Course' ),
'name' => __( 'Name' ),
'phone' => __( 'Phone' ),
'amount' => __( 'Amount' ),
'organization' => __( 'Organization' ),
];

return $columns;
}

function manage_course_payment_columns( $column, $post_id ) {
switch ( $column ) {
case 'payment_date':
echo '<a href="' . get_edit_post_link( $post_id ) . '">' . get_post_meta( $post_id,
'sale_created',
true ) . '</a>';
break;
case 'course':
$courseId = get_post_meta( $post_id, 'course_id', true );
if ( $courseId ) {
$course = get_post( (int) $courseId );
echo '<a href="' . get_edit_post_link( (int) $courseId ) . '">' . $course->post_title . '</a>';
} else {
echo __( 'No course linked to payment', 'lam' );
}
break;
case 'name':
$userId = get_post_meta( $post_id, 'user_id', true );
echo '<a href="' . get_edit_user_link( (int) $userId ) . '">' . get_post_meta( $post_id,
'buyer_name',
true ) . '</a>';
break;
case 'phone':
echo get_post_meta( $post_id, 'buyer_phone', true );
break;
case 'amount':
echo get_post_meta( $post_id, 'price', true ) . ' ' . get_post_meta( $post_id, 'currency', true );
break;
case 'organization':
$userId = get_post_meta( $post_id, 'user_id', true );
$organization = get_user_meta( $userId, 'organization', true );
if ( $organization ) {
echo get_the_title( (int) $organization );
}
break;
}
}


Bob comments:

function filter_payments_by_course( $query ) {
global $pagenow;
// Get the post type
$post_type = isset( $_GET['post_type'] ) ? $_GET['post_type'] : '';
if ( is_admin() && $pagenow=='edit.php' && $post_type == 'course_payment' && isset( $_GET['course'] ) ) {
$query->query_vars['meta_key'] = 'wisdom_plugin_slug';
$query->query_vars['meta_value'] = $_GET['course'];
$query->query_vars['meta_compare'] = '=';
}
}
add_filter( 'parse_query', 'filter_payments_by_course' );


Bob comments:

I was editing code and submitted comment.

can you please try this?


function filter_payments_by_course( $query ) {
global $pagenow;
// Get the post type
$post_type = isset( $_GET['post_type'] ) ? $_GET['post_type'] : '';
if ( is_admin() && $pagenow=='edit.php' && $post_type == 'course_payment' && isset( $_GET['course'] ) ) {
$query->query_vars['meta_key'] = 'course_id';
$query->query_vars['meta_value'] = $_GET['course'];
$query->query_vars['meta_compare'] = '=';
}
}
add_filter( 'parse_query', 'filter_payments_by_course' );


do you know if your site has any other "parse_query" filter.


hamergil comments:

Unfortunately this also not working
I get this results:
http://prntscr.com/zn9ibc
also the filter is not showing the courses in the results page

and NO. I don't use any other parse_query in the site
any idea why none of the answers above working?


hamergil comments:

This is the trail URL by the way
edit.php?s&post_status=all&post_type=course_payment&action=-1&m=0&course=519&filter_action=filter&paged=1&action2=-1
if it helps...


Bob comments:

Will it be possible to give me access of your site/ftp? So I can check and fix it.

Email [email protected]


hamergil comments:

Unfortunately not. I can give you access to my local machine if it's OK
we can do it via Teamviewer or something similar..
or maybe clone the site to another FTP where we can play there with code


Bob comments:

Cloning to another ftp is good.

Let's do it.


hamergil comments:

Client won't allow me oto do it unfortunately..


Bob comments:

Can we try to use your localhost via anydesk?

2021-02-16

Arnav Joy answers:

is there any other query/filter applied for this post type?


hamergil comments:

with post type course_payment?
No