Best practice custom function, where to echo the variables?

Generically speaking, yes. If your normal output would be to echo at the end, echoing during the loop wouldn’t change much. function get_some_posts($args) { $query = new WP_Query( $args ); if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); the_title(); // the_title() is the same as echo get_the_title(); } } } add_action(‘custom_hook’,’get_some_posts’); The … Read more

Sort by two dates. Default entry date and custom field if present

Few issues I see here with your code: You have category_name set to upcoming class … category slugs do not have spaces in them, so this is incorrect. If you meant to have all upcoming and class categories, then use upcoming,class https://developer.wordpress.org/reference/classes/wp_query/#category-parameters You have a meta query on company but it sounds like you’re also … Read more

Meta-value query

The string you are outputting using print_r() is what is known as a serialized string. When WordPress stores an array of values in 1 custom field, it compresses the array into this string using a function called serialize() What you want to do is to unserialize the string, and then you can access each “associated … Read more

Modifying raw $_POST data early in the lifecycle

Yes, there are points during the WordPress request life-cycle that provide hooks for you to make modification to the request and other parameters available, more on that in just a moment. Consideration… You can modify superglobals, as you are aware, but in doing so you may be introducing unexpected behaviour into your application especially if … Read more

How do you query wordpress posts using a math formula between multiple meta field values?

I think the easiest way would be to create a third meta field for “score” and use the WordPress update_post_meta() function to automatically calculate its value, based on the values of assignment_value and current_grade. Exactly how to do that has been answered well in this response: https://wordpress.stackexchange.com/a/54068/16 Once you have your third “Score” meta field, … Read more

Include Sticky Posts with Custom Query

$sticky = get_option( ‘sticky_posts’ ); $args = array( ‘post_type’ => ‘post’ , ‘orderby’ => ‘date’ , ‘order’ => ‘DESC’ , ‘posts_per_page’ => 4, ‘cat’ => ‘3’, ‘paged’ => get_query_var(‘paged’), ‘post__in’ => isset( $sticky[0] ) ? $sticky[0] : array(), ‘ignore_sticky_posts’ => 1, ); $q = new WP_Query($args); The above code will include only the first sticky … Read more

How to display the number of articles published per user

If you have access to user IDs, you can use the count_user_posts() function. You would get the number of posts by a user like this: //Assume the variable $thisUser is equal to a valid user ID $ThisUserCount = count_user_posts($thisUser, ‘recipe’); EDIT: Where you’re calling the count_user_posts() function, you’re passing it the array $args instead of … Read more