Warning: Undefined variable $post_id

Try this instead: $posts_with_category = new WP_Query( $args ); if( $posts_with_category->have_posts() ) { Instead of just checking if $posts_with_category is non-empty it checks if it’s an array of WP_Post objects. We don’t have line numbers to see where the error is being thrown. Is line 47 your return statement?

How to check if the post exists in any of the categories?

Since you are doing this for a custom taxonomy (solution_cats), then you should instead use has_term() and not in_category() which works only with the default/core category taxonomy. So try this with your second code: has_term( $category->slug, $category->taxonomy, get_the_ID() ) is_object_in_term() can also be used, but your syntax was not correct – the 2nd parameter is … Read more

Set Featured Image of a post

To set a featured image when creating a post using wp_insert_post(), you can use the set_post_thumbnail() function // Create the post $post_id = wp_insert_post($args); // Check if the post was created successfully if (!is_wp_error($post_id)) { // Set the featured image if ($image_url) { // $image_url should be the URL of the image you want to … Read more

Why does ‘get_the_ID’ work outside of the loop?

The get_the_ID() function uses the global $post variable, which outside of the loop is sometimes equal to get_queried_object_id(). Within the loop, the $post variable is set to the current post within the loop. As to why get_queried_object_id() is recommended outside of the loop, is because it does not use the global $post variable, so is … Read more

Modify upload directory to use post category slug in file path on multisite installation

try this: function add_taxonomy_to_upload_dir( $upload ) { if ( isset( $_REQUEST[‘post’] ) ) { // wp-json/wp/v2/media $post = get_post( $_REQUEST[‘post’] ); } elseif ( isset( $_REQUEST[‘post_id’] ) ) { // wp-admin/async-upload.php $post = get_post( $_REQUEST[‘post_id’] ); } else { return $upload; } if ( ! $post || empty( $post->ID ) ) { return $upload; } … Read more

Display posts of only a certain category in WP Admin section?

You can filter the posts list by appending ?category_name=xx to the admin posts list URL, and you can add a submenu page with that URL as the target via add_submenu_page: add_action( ‘admin_menu’, ‘wpd_admin_menu_item’ ); function wpd_admin_menu_item(){ add_submenu_page( ‘edit.php’, ‘Page title’, ‘Menu item title’, ‘edit_posts’, ‘edit.php?category_name=somecat’ ); }

Display related posts randomly that match “any” existing tags?

Some of the code in OP is a bit old and depreciated, like caller_get_posts which was depreciated years ago. The correct parameter to use now is ignore_sticky_posts. Your query is also really inefficient and not good for performance. Here is how I would tackle this issue Use get_queried_object_id() to get the current post ID instead … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)