Converting MYSQL to WordPress $WPDB

The root problem here is the assumption that WordPress deals in mysql functions and mysql objects, and that knowledge of these functions is helpful. The truth is, this is a complete API in of itself, and so the assumptions that knowledge of mysql_ functions provides are not useful or relevant beyond the syntax of SQL. … Read more

Link to the parent post in attachment page

Here’s one way within the main loop on the attachment’s page: First we get the parent ID from the current attachment’s post: $parent_id = get_post_field( ‘post_parent’ ); Then we check if it’s non-zero and print the link to the parent: if ( $parent_id ) { printf( ‘<a href=”https://wordpress.stackexchange.com/questions/312468/%1$s”>%2$s</a>’, esc_url( get_permalink( $parent_id ) ), esc_html__( ‘Go … Read more

dynamic php inside wordpress page

What you can do is to use a shortcode, for example, you can add them to your functions.php or create a plugin that does the same. Check this guide, but keep in mind there is a lot of information about WordPress and shortcodes. In the end, you will only have to call your shortcode wherever … Read more

How to prevent those PHP variables from being cached on WordPress?

Page caching generates a static HTML version of the page so IP geolocation occurs on the first rendering and is not triggered on subsequent renderings – your use case is not compatible with W3 Total Cache’s page caching. It’s possible to accomplish what you are trying to accomplish with page caching, however, you would need … Read more

Use wp_get_recent_posts with search term

It should just be a matter of setting the ‘s’ argument of wp_get_recent_posts() (or just get_posts()) to the search term: $args = array( ‘numberposts’ => $numberposts, ‘offset’ => $offset, ‘category__not_in’ => array(391), ‘category’ => $category, ‘orderby’ => ‘post_date’, ‘order’ => ‘DESC’, ‘post_type’ => ‘post’, ‘post_status’ => ‘publish’, ‘s’ => $searchterm, ); $recent_posts = wp_get_recent_posts( $args, … Read more

IMG src weird behaviour inside a single post loop

the_post_thumbnail function display the post thumbnail i.e. <img> tag. You don’t need to use <img> tag when using this function. Try this code: <?php if (have_posts()) : while (have_posts()) : the_post(); ?> <article class=”post”> <h2 class=”postTitle”><?PHP the_title(); ?></h2> <?php the_post_thumbnail( ‘predefImageSize’, [‘class’ => ‘singleMainImg’, ‘title’ => ‘Feature image’] ); ?> <p class=”postinfo”>Created on <?php the_time(‘F … Read more

Ajax $wpdb not returning table data

The main problem, I see in your code, is that you use query method of wpdb and assume that it is PDO statement object… But it is not. Take a look at wpdb Codex: https://codex.wordpress.org/Class_Reference/wpdb query method is used to run custom queries on DB – that’s all. What you want to use is get_results. … Read more