New answers tagged

1

Here's one way by using wpdb::get_col() and fetch the id column: $pids = $wpdb->get_col( "SELECT id FROM my_table" ); We could then clean it by integer convert each id with: $pids = wp_parse_id_list( $pids ); Just note the different max value for intval(), depending on 32 or 64 bits systems.


1

post__in accept array of post IDs e.g. array(5, 4, 8, 9); Extending @bravokeyl comment. By default result is Object. As codex suggest you can change the output type of get_results() to these values. OBJECT - result will be output as a numerically indexed array of row objects. OBJECT_K - result will be output as an associative array of row objects, ...


0

The WP_Query() custom field (i.e. meta) query can handle arrays for field values. You just need to add the compare key to your array: $args = array( 'numberposts' => -1, 'post_type' => 'post', 'meta_query' => array ( array ( 'key' => 'my_key', 'value' => 'target_value', ...


1

Your code is invalid (syntax error, missing closing array and semi-colon), and incorrectly nested - if you were to properly indent your code, you'd have something like: $args = array( 'tag' => 'tag-AAA,tag-BBB', array ( 'orderby' => array( 'title' => 'ASC', 'post_date' => 'DESC', ...


0

You need to use the methods of your query instance, rather than the global functions (which are merely wrappers for the global $wp_query): if ( $query->have_posts() ) { $term = $query->queried_object; while ( $query->have_posts() ) : $query->the_post(); //Output my posts the_title(); the_content(); endwhile; ...


2

I think you should be able to do this with the user_has_cap filter. Because that filter passes through the post ID in the $args for the edit_post capability, you can use that to check if the current user has been artificially given this capability - and if so, force the edit_post capability to be true for that instance. Building from the example in the ...


2

WordPress will cache all terms for taxonomies attached to all post types in the query result set by default - well, so long as you haven't set either cache_results or update_post_term_cache to false (codex). So calling get_the_terms etc. within the loop will not hit the database. However, wp_get_object_terms will hit the database as it by-passes WordPress' ...


2

If I've understood your intentions correctly here, you don't need to use the meta_value argument at all. If you are just wanting to order by this meta_key but not filter which results are returned - i.e. you want to return the results from all the 'star' values - then providing the meta_key and the orderby arguments will get you there. There is no need for ...


2

There is no order_by. Change order_by to orderby and it should work. The default value of compare is = so it is unnecessary, you can remove it. Also, you should specify type of the meta_value if it is a specific type rather than the default CHAR.


0

I think one of your query not satisfying the condition. Which condition you have removed to make the query run? The last condition 'by owner'? If so, try the following meta_query If something wrong with the condition change the relation of the query from AND to OR and check (Refer). Also echo your query and run in PHPMyAdmin to make sure that there is no ...


0

It is really hard to make any specific suggestions without a more detailed knowledge about your specific situation, but whenever I am stuck with a site that is slow for no good reason, I install Query Monitor plugin (https://wordpress.org/plugins/query-monitor/) and look closely at what it says. I use Advanced Custom Fields extensively, and more often than ...


1

With is_paged you are testing whether a post has been divided in parts using the QuickTag. But with next_posts_link you are pointing to the next page of posts. So you probably need another test.


0

The code is fine, I can't see any need to modify the query - I think you're missing the third argument in your get_post_meta call: $postACF_sc = get_post_meta($postId, 'sc_link', true /* $single */); You might also want to add this snippet just before the foreach to save multiple database queries: update_postmeta_cache( wp_list_pluck( $posts, 'posts_id' ...


0

To get the custom fields for a post you would need to add something like the following: d.meta_value (in the select clause) LEFT JOIN '.$var_setting['table_postmeta'].' d ON d.post_id = b.ID (in the from clause) I don't know the plugin so I can't promise it will work but this is how you join to the postmeta table to pull the custom fields. Hope ...


1

This is definitely not within realm of native orderby parameters in query. They are limited to an order by individual meta fields and have no ways to implement aggregate of them. You would have to either write custom SQL for it or query a broader set and finalize calculations on PHP side. It is hard to guess which would be more fitting, since you don't ...


3

According to https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters you want to specify it similar to the following code example: $args = array( 'orderby' => array( 'title' => 'DESC', 'menu_order' => 'ASC' ) ); $query = new WP_Query( $args ); This is supported from WordPress 4.0. For ...


0

That's what works for me (showposts): $query = new WP_Query(array( 'showposts' => 4, 'post_type' => 'xxx', 'cache_results' => false ));


4

That's because the main query is being discarded and replaced with your custom query. You've not told your custom query to look for that tag, so why would it? You might also notice your pagination is broken for the same reason, you've not told the new query which page you're on, so why would it pull the right page? What's more, that main query is expensive! ...


0

MP3 Playlists have a .m3u extension. I'm guessing that your Media Library is not registering the MP3s but the playlists instead, so they may physically be in that location, but not in the database. One work around if you know the names of the files could be to try using the PHP id3_get_tag() function. Read more here: ...


0

Figured this out. 'order' => 'ASC' (or DESC - however you'd like) and 'orderby' => 'menu_order' solves this.


0

I have added extra rewrite rules for pages with no item_width and item_height in combination with pagination as follows: /** * Adds rewrite rules for shop category */ function gtp_add_shop_rewrite_rules() { // shop-category/category/item_width/item_height/page/2/ add_rewrite_rule( '^shop-category\/([^/]*)\/([^/]*)\/([^/]*)\/page\/([0-9]*)$', ...


1

If we use: $args = array( 'post_type' => 'custom_post_type', 'post_status' => 'publish', 'posts_per_page' => 9, 'meta_key' => 'pkg_id', 'orderby' => array( 'pkg_id' => 'ASC', 'rand' => 'DESC' ) ); then we get the order part as: ORDER BY wp_postmeta.meta_value ASC, RAND() DESC This seems to ...


5

There is no need to do an overrated amount of queries to accomplish this. you can still use only one query, the_posts filter and some PHP to sort your code as needed. I take that this is a custom query from what I read in your question, so we can do the following: First, we want to introduce a custom WP_Query parameter so that we can use that parameter ...


1

Not possible with a single query you would have to do 3 seperate query each one targeting the different "Package ID's" and ordering those queries as rand. $args1 = array( 'post_type' => 'custom_post_type', 'orderby' => 'rand', 'meta_query' => array( array( 'key' => 'pkg_id', 'value' => 1, // you ...


0

It might be status issue. Try with: 'post_status' => 'any'


0

You don't have to merge the arrays to use them together in a loop. You just need a similar key that you can use as a hash lookup. That's where wp_list_pluck comes in handy. // Image Data $instagrams = array ( array ( 'post_id' => 1, 'image' => 'http://placekitten.com/200/300', ), array ( 'post_id' => 2, ...


0

I started this in a comment but thought I'd expand it for you because I do like the question ;) I'd probably tackle this by adding a hidden field to the search form with the current page ID, and then using the pre_get_posts filter to modify the search query to only return pages with a matching parent. However: I don't think we can deal with an additional ...


2

Technically you can merge things into loop, by manipulating its posts field (such as $wp_query->posts for main query). WP has it in public for visibility and access, as it tends to. However it's not that common technique, less so if you are considering injecting something that is not actually posts. It is more common for such output to check and output ...


0

Figured it out. The Answer: // tax_query needs to be an Array of an Array 'tax_query' => array( array( 'taxonomy' => 'vodka_type', 'field' => 'slug', 'terms' => $type, ), ),


0

Sorry for the delay on this everyone, I was out of town! Here is what I figured out and am using today. I had to check for the cases where one taxonomy was selected and one was not, which actually only requires a simple tax query, but that was giving me null values and breaking the query, so I had to use some conditionals instead to build it. $typeIDs and ...


1

As @Milo suggested and also codex documented it is_main_query Under_the_Hood This function is an alias for the method WP_Query::is_main_query(). In filter or action hook callbacks that are passed the WP_Query object, such as pre_get_posts, it is circular to call this function. Instead, directly call the passed object's method. For example, if ...


0

If I follow your description right you want posts which have any $typeIDs, but all of $unionIDs ? Aside from wrong meta_query choice, you miss operator argument which specifies which kind of match you want. I think your query should be something like this: 'tax_query' => array( 'relation' => 'AND', // you want both conditions to match array( ...


2

I call it when I render a metabox on the edit page. ( Yeah, you shouldn't be using globals in this context - use a foreach loop instead: function get_available_attachments( $post /* This is the post object being edited */ ) { $attachments = []; $attach_query = new WP_Query([ 'post_type' => 'attachment', 'post_status' ...


-2

Try this code, meta query will not give you the results you want. Please go through the meta query for more details. Meta query Or go here for reference. <?php $myquery = array( 'numberposts'=>-1, 'tax_query' => array( 'relation' => 'OR', array( ...


0

This question is more than 1 year old, but I have the same problomlem, and here is function that will add each meta_value and meta_key to $wp_query object, instead of query each post meta in while loop, this function will do One extra query example: "SELECT meta_key, meta_value, post_id FROM $wpdb->postmeta WHERE post_id IN (1,2,3,4,5...)" where ...


1

You may want to check this: How do I query all posts of one type across my multisite installation? Just change post to media and you'll be fine.


-2

<?php global $post; $child_pages_query_args = array( 'post_type' => 'page', 'post_parent' => '93', 'orderby' => 'date DESC' ); $child_pages = new WP_Query( $child_pages_query_args ); ?> <?php if ( $child_pages->have_posts() ) : while ( $child_pages->have_posts() ) : $child_pages->the_post(); ?> <div ...


1

You are setting null to $wp_query then querying get_query_var( 'paged' ) this is expected that you will get this error! First query get_query_var( 'paged' ) then set the original $wp_query to null Example:- $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; global $wp_query; $temp = $wp_query; $wp_query = new WP_Query( ...


2

The solution is to use: 'taxonomy' => 'meal', 'field' => 'slug', 'terms' => 'breakfast' The taxonomy and terms are obvious, but why does field have to be slug? When you add taxonomy parameters, you can specify what 'breakfast' is-- via the field. It could be the term's ID, the full name, or slug. See the codex on Taxonomy Parameters


0

The first thing I would recommend is verify the number of posts your query is returning using the following: $apartquery->found_posts


4

the first request doesn't show any custom post type (polylang_mo is a custom post type) polylang_mo is a private custom post type so unless I explicitly request this post type, it won't be included. the second request shows only 1 page (instead of 3) Polylang was actually filtering the results. Adding --skip-plugins=polylang makes the 3 posts ...


0

So I finally figured it out. Using pre_get_posts and the $_GET global var to check if any query args have been added works a treat. I used the follow code to get the desired results. function my_pre_get_posts($query) { if( $query->is_main_query() && is_archive() && !isset($_GET['orderby']) && // ...


0

So I decided to go with "load more" clicking option but the code I"m using doesn't work, click on load more and nothing happends. Index.php: <div id="content"> <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $args = array( 'multisite' => 1, 'sites__not_in' => array(1), 'paged' => $paged,); $loop = new ...


0

Infinite scroll usually requires some kind of Javascript library to load more results via Ajax. The basic idea is this: User arrives at your page, you show, say, 5 posts per page They scroll past a certain offset and you use JavaScript to trigger an Ajax request Your request passes the requested paged, the endpoint receives the request, and returns just ...



Top 50 recent answers are included