Tag Info

New answers tagged

0

I have solved my problem and it had nothing to do with the query, the database or the post type. The problem was that in trying to initially construct the query I had left a pre_get_posts function in my functions.php which was conflicting with my query. The lesson here for me is to make sure I have thoroughly checked ALL files. The offending counter code: ...


0

You need a loop within a loop - loop through a counter, and for each value, check for the existence of each key with the current counter value appended. // max custom field index $number = 40; // the counter $counter = ''; // the meta keys to check for $keys = array( 'custom_text', 'custom_image', 'custom_video' ); // all our custom field values ...


1

I'm posting a new answer because I found this thread while searching for an answer and the solutions here weren't optimal. The post_type argument can be a string or an array of post types. function custom_count_posts_by_author($user_id, $post_type = array('post', 'page')) { $args = array( 'post_type' => $post_type, 'author' => ...


0

Sorry for answering my own question but it may help some other devs too. I created this additional filter 'json_query_var-meta_query'that returns the necessary arguments. function adjustQrry($data){ $args = array(); $args['relation'] = 'AND'; foreach ($data as $key=>$value) { if ( 'relation' === $key ) { ...


0

Switching out echo get_comments_number($post->ID) for echo $post->comment_count did the trick. I have no idea why, that's exactly what the get_comments_number() function does...


1

You can modify search query and set your parameters with the help of pre_get_posts. Like this. function wpse_custom_get_posts( $query ) { if ( is_admin() || ! $query->is_main_query() ) return; if ( $query->is_search() ) { $query->set( 'post_status', 'publish' ); } } add_action( 'pre_get_posts', 'wpse_custom_get_posts', 1 );


-2

You can also use this method.. $total_comments = wp_count_comments( $post_ID ); echo "Total Comments: " . $total_comments->total_comments;


-3

$current_post_comments_count = get_comments_number( $post_id ); You can use this.


0

Instead of that try using; <?php get_userdata( $userid ); ?> For example; <?php $user_info = get_userdata(1); // get info for user id '1' $username = $user_info->user_login; $first_name = $user_info->first_name; $last_name = $user_info->last_name; ?> For more details visit this link.


0

Because I am a complete idiot, I seem to have completely forgotten that I basically answered my own question over a year ago. I was struggling to get the posts_join filter to behave (I generally have trouble with INNER JOINs anyway), and so instead looked to the posts_where filter. The main issue with it is that you can't pass any arguments to it other ...


1

The answer from the OP inspired me to quickly write this function which is not resource intensive and really fast. I also decided to try another method as described by the OP To accomplish this, I've made use of the Transient API and the post_updated action THE LOGIC To get the position of a post, you will first need to get all the posts and determine ...


1

Depending on your timeline or how much you want to live on the edge, there were some great improvements that are set to be introduced to 4.1 that I think will allow you to do just what you're looking to do. If you want to live on the edge a little, you could develop with the 4.1 beta and switch to the release at the time of launch (be sure to file bug ...


0

You can't figure out a way to do that because there is no such way. It is not possible to mix ANDs and ORs like that with meta_query. The query system is great, but not all inclusive. You will need to use multiple queries, or filter the SQL yourself.


1

Thanks @PieterGoosen and @Milo for the suggestion of doing everything back-end. Found an answer here and adjusted it to use get_posts and suit my needs. My final code is: function updateNumbers() { $options = get_option('theme-opts'); $opts_cat = isset($options['opts-category']) ? $options['opts-category'] : ''; $args = array( 'posts_per_page' => ...


2

In the meta_query arguments, try changing your 'type' => 'CHAR', to 'type' => 'DATETIME', You can opt for any one of these, depending your needs: type (string) - Custom field type. Possible values are 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'. Default value is 'CHAR'. But ...


0

After giving it another look with some fresh eyes, I decided to add a third loop within Loop 2 and not have it working as desired: $media_args = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'tax_query' => array( array( 'taxonomy' => 'products', 'field' => 'term_id', ...


1

OK - I got it, the old posts already on the database do NOT have an english translation and that's the problem with my loop. When I add a new post, I have the option to provide a translation which then appears in the english page. looking through the docs I found that I can also show the default when there is no translation by something like : <?php ...


0

Looks like all I needed was to include 'posts_per_page' => -1 in the $query_args array. Final, working code: <ul class="tag-list"> <?php $query_args = array( // Easier to call the parent category, "recipe" instead of dealing with all these ids // 'cat' => '101,94,93,56,72,99,100,63,98,95,96,80,4', ...


0

Thanks to @Howdy_McGee. He gave me the hint to look after serialized meta queries. With this code I get the desired result. $args = array( 'posts_per_page' => -1, 'orderby' => 'post_date', 'order' => 'DESC', 'post_status' => 'publish', 'meta_query' => array( array( 'key' ...


1

As your data is currently structured, you can't use SQL to do that (well, not in a performant way, at least). You have two options: Fetch ALL posts and loop through them in memory looking for the correct post meta, or Store the meta values separately. Between the two, I advise the latter. WordPress permits you to store multiple rows of the same post meta ...


1

I don't believe you can accurately compare values for a Serialized Array, which happens whenever you pass an array to update_post_meta, you need to get the entire value during your loop using get_post_meta( $post->ID, 'enterprise_on_page', 1 ) If you need to specify queries based on these values it's best to save them out separately. View this ...


3

Static pages doesn't work with paged query variable, they need the page variable. This is the reason why your second code block makes the query work: it uses the page var when available. However, your paginated links code always use paged: ... 'format' => '?paged=%#%', ... So you are sending paged query var but looking for page var. Solution is to ...


1

Ids 7 & 11 are probably sticky posts. Try this: $args = array( 'post_type' => 'page', 'post__in' => array(24, 26, 28), 'ignore_sticky_posts' => 1 );


0

I'm not sure if I get you right, but if you're trying to query by multiple meta keys where the number of meta keys varies, then below is what I am using for the same purpose. Basically it adds meta query one after another if the corresponding input is existent. <form action="" method="post"> <input name="item_name" /> <select ...


0

You can modify the query using the pre_get_posts hook, you will have to add the parameters of your query to the query provided by WordPress inside you callback function (exclude_category in the example below): Example from the WordPress Codex: function exclude_category( $query ) { if ( $query->is_home() && $query->is_main_query() ) { ...


0

A meta_value field is always text; the fieldtype in the database structure is longtext, so unless you're explicitly typecasting when retrieving it (i.e. $price = (float)$custom['price'][0]; - which won't help with ordering in the first place), you can't 'adjust' the custom field data - it's always stored as text. Having said that, you should simply remove ...


0

As noted by @s_ha_dum, I had some sticky posts. I had to use the 'ignore_sticky_posts' => true argument inside the WP_Query.


3

I'm not sure I understand the question, but if you just wan't do display something from resultset B on every nth iteration, you could use next_post() or simply call the_post() on the second query object. Of course you also need to check so there's enough items left in the result and so on: $query1 = new WP_Query(array('post_type' => 'post')); $query2 = ...


1

I think that you can improve performance saving view information in one single option instead in post meta applying the filter date on saving instead of on retrieving create the markup and save in a transient instead of saving the query (fragment caching) Rough implementation in a class class PopularPosts { const OPT = 'myplugin_popular_posts'; ...


0

Your query won't work as it will simply ignore your taxonomy query. This is not a bug but expected behavior. When using p as your parameter, every other query parameter are ignored/bypassed like for example post_type, tax_query and any taxonomy related queries like category and tag parameters due to the fact that you are directly querying specific posts by ...


0

The final code I used was: <?php $slides_category = get_field('slider_category'); $args = array( 'post_type' => 'slides_post_type', 'tax_query' => array( array( 'taxonomy' => 'category', 'field' => 'id', 'terms' => array( $slides_category ), ), ), ); $slides_loop = new ...


3

EDIT From your comments, I have reworked the entire solution. I'm keeping the original answer just for reference If I have 3 sticky posts, shouldn't they be the first 3 posts on the first page, regardless of their post date? What's happening is that only the sticky posts that happen to be in that first set of 10 posts based on their date are treated as ...


-2

Use wp_reset_query() after first query, on 3th code line $args = array(- clear extra -


4

If we define the conditions: A: my_custom_field_ignore EXISTS B: my_custom_field_ignore = 1 then NOT ( A && B ) is equivalent to: NOT ( A ) || NOT ( B ) meaning in our case: ( my_custom_field_ignore NOT EXISTS ) || ( my_custom_field_ignore != 1 ) We could therefore try the following for WP 3.5+ (untested): $args = array( 'post_type' ...


1

There's a problem with your get_page_by_title() usage, since you assume it returns the page ID, but it returns an object/array or null. You also have to tell it about your custom post type. I also assume your products are stored as custom post type cars. Please try the following with a given product title (untested): // DEBUG: Let's first try out an ...


0

Thank you for answering!. I was looking for a more complete solution. I figured it out. echo '<div class="row profiles">'; $terms = get_terms("clinical-programs"); if ( !empty( $terms ) && !is_wp_error( $terms ) ){ foreach ( $terms as $term ) { //View faculty member by clinical programs ...


0

You could do it with something like this in theme functions.php (at end) add_action('admin_menu', 'news_admin_test'); function news_admin_test() { add_menu_page( 'News Page Title', 'News Option', 'manage_options', 'newspage', 'show_menu_news', get_home_url() .'/wp-content/themes/my_theme/assets/img/logo.png' ); } function show_menu_news () { echo ...


0

which gives me all posts inside the tax inspiration - I need to alter this, so that I get only selected ID's in my array, ex. 4714, 3608, instead of all terms of the tax. I'm assuming you mean that this query gives you all posts inside post type inspiration and you want to pull specific post IDs instead of all posts of the post type. Taxonomies and Post ...


0

list all the PAGES that are assigned to particular page(s). I'm assuming you mean "particular term(s)." which would be in your clinical-programs taxonomy. You'll have to do a bit of coding - you need to start a new WP_Query and structure your query as such: $taxPages = new WP_Query( array( 'post_type' => 'YOUR_POSTTYPE_HERE', 'clinical-programs' ...


0

Do you mean the featured image? Use wp_get_attachment_image - Here is the code that I use in my plugin while ( $the_query->have_posts() ) :$the_query->the_post(); $image_id = get_post_thumbnail_id(); $imagesize="thumbnail"; $image_url = wp_get_attachment_image_src($image_id, $imagesize, true); //do something endwhile; You can also use ...


0

First of all, get all term slugs from the custom taxonomy space by current post ID. $space_terms = wp_get_post_terms( $post->ID, 'space' ); if( $space_terms ) { $space_terms = array(); foreach( $space_terms as $term ) { $space_terms[] = $term->slug; } } You should specify the logical relationship between each inner taxonomy array when there ...


0

SELECT * FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id) LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id) LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) LEFT JOIN ...


1

Try This code <?php echo '<ul class="nav nav-tabs" role="tablist">'; $args = array( 'hide_empty'=> 1, 'orderby' => 'name', 'order' => 'ASC' ); $categories = get_categories($args); foreach($categories as $category) { echo '<li> <a href="#'.$category->slug.'" role="tab" data-toggle="tab"> ...


6

There is a sad truth: you can never ever be sure that some code will not break your code, and there is nothing you can do to prevent that. Expecially in WordPress where everything is global. That said, yes, global $post is one of the most used global var, so using special care for it can be a good idea. In my code I rarely direct access to global $post. ...


1

I'm not in the position to code right now, but here is an idea A thousand posts is quite a lot, but it is even more if you are retrieving all posts from the db in one go. What if you have ten thousand posts, of which only ten or twenty is not tagged after the whole retagging operation. This can become such a huge query that you run the danger of timing out ...


0

@Milo found that the problem was with the code to order by the track number taxonomy. I'm going to make track number a custom field.


1

$search_term is already an array of an array. When you eventually add that to your meta_query, you get an array of an array of an array, which will not work You are using AND as your relation operator which is the default for a multi-array meta_query. I would suggest that you drop that, and then simply just feed $search_term directly to your meta_query. No ...


1

Before I start, you have a problem with your meta_query. You should not use relation for a single inner meta_query array relation (string) - The logical relationship between each inner meta_query array when there is more than one. Possible values are 'AND', 'OR'. Do not use with a single inner meta_query array. Your first query seems to be correct and ...


0

Your best option is to use a taxonomy. If EN, DE and FR are terms then you then loop through them and get the pages on a per term basis. You can do this in conjunction with a plugin that enables you to manually re-order terms such as this one. https://wordpress.org/plugins/i-order-terms/ You can then do something like this with your code: // Get all of ...


0

Some thoughts on how I would approach this. I can update later with some code if necessary when I have the time. I would handle the first page with two additional queries, so three loops on page one. Hook pre_get_posts and do two meta queries for the three posts that will appear in the first two loops. Those will be cached for later when you display them on ...



Top 50 recent answers are included