WordPress hide post from custom post-type on a single page

Instead of using the pre_get_posts hook simply modify your WP_Query to fetch posts with certain conditions. $loop = new WP_Query( array( ‘post_type’ => ‘projects’, ‘orderby’ => ‘rand’, ‘posts_per_page’ => 1, ‘meta_query’ => array( array( ‘key’ => ‘your_field_name’, ‘value’ => ‘the_value’ ), ), ) ); This, right from the beginning only fetches the posts that matches … Read more

Adding featured image to a new post using front-end form?

Updated I’m not sure how to do it without ContactForm 7 plugin, but here’s my past code on how to do it in CF7: Assuming your file field is [file your-file]. add_action( ‘wpcf7_before_send_mail’, ‘my_cf7_save_featured_image’, 10, 2 ); function my_cf7_save_featured_image( $instance, $result ) { require_once( ABSPATH . ‘wp-admin/includes/image.php’ ); require_once( ABSPATH . ‘wp-admin/includes/admin.php’ ); // Get … Read more

How to get post image caption by index

I have used function to extract data inside figcaption tags, and $position is stand for the index of the current image. function getImageCaption($postID, $position){ $post_id = $postID; // get the post object $post = get_post( $post_id ); // we need just the content $content = $post->post_content; // we need a expression to match things $regex … Read more

Use PHP Class in WordPress functions

If you have defined the class MyCart directly in functions.php with your hooks. I’d expect for the following to work: // add action $cart = new MyCart(); function emdr_add_to_cart(){ $items = $cartClass->get_users_cart_contents(); // process items } add_action( ‘woocommerce_add_to_cart’, ’emdr_add_to_cart’); I might also add that duplicating the “state” of the users cart in 2 places is … Read more

I want to show cart items number if any or nothing, simple CART text

You’re already on the right track. Simply add conditional if 0 <?PHP $cart_count = WC()->cart->get_cart_contents_count(); $cart_count = $cart_count ? “({$cart_count})” : ”; // if 0, become empty string $cart_link = wc_get_cart_url(); $cart_button = “<a href=”https://wordpress.stackexchange.com/questions/378487/{$cart_link}”>Cart {$cart_count}</a>”; echo $cart_button;

display most active user sort by comment count [Solved]

you can use this $user = new wp_user(get_current_user_id()); function commentCount() { global $user; global $wpdb; $count = $wpdb->get_var(‘SELECT COUNT(comment_ID) FROM ‘ . $wpdb->comments. ‘ WHERE comment_author_email = “‘ . $user->data->user_email . ‘” and comment_approved = 1’); return $count; } echo ”; echo commentCount();