Tag Info

New answers tagged

1

You are interfering with the main query/loop, which is bad - there is nothing more to say. You can do a custom loop, see the codex article The Loop to start with, or use the pre_get_posts hook, if you want to do something other then the usual behaviour. If you need to toy with $wp_query, for example to fix the pagination on custom loops, then restore it ...


0

So this is what I needed: <?php $terms = get_the_terms( $post->ID , 'board' ); foreach ( $terms as $term ) { echo $term->name; } ?>


3

Build an array of categories: foreach ($categories as $category) { $cat[] = $category->cat_ID; } Then pass that to the query: $args=array( 'category__in' => $cat, 'order' =>DESC, 'post__not_in' => array($post->ID), 'posts_per_page'=>2, ); In other words, put the query after the foreach, not inside of it.


0

$i is being reset for each category. Try declaring $i before the foreach cat loop: <?php $i = 0; $categories = get_the_category(); // etc And if $i = 0, then you will get 3 posts before it stops, so try this: if( $my_query->have_posts() ) { while ($my_query->have_posts() && $i < 2) // etc


0

All of the comparison operators you list operate on whole strings except for LIKE, NOT LIKE, REGEXP, NOT REGEXP,RLIKE. However, using those operators is going to create very inefficient queries, and potentially unreliable ones. You are fighting with some of the basic design of MySQL with what you are doing. Save your "availability" key as many times as you ...


0

You may be able to use the LIKE operator: $meta_args[] = array( 'key' => 'availability', 'value' => $availability, 'compare' => 'LIKE' ); In the codex you can see a list of comparison operators.


0

To get only child pages (pages that have parent selected), you can use this query: http://example.com/api/get_posts/?post_type=page&posts_per_page=1&orderby=rand&post_parent__not_in[]= And that should also give you the answer for your second question, I guess ;) If you want to pass array(1=>'foo', 2=>'bar') as param called my_array, you ...


0

I see three options here, but before I start, just a few notes on your code Do not use $wp_query as your query variable. This is the global query variable used by the main query which also holds the main query object. The way you have used it breaks the main query object and sets your custom query to it. You should avoid this method at all times as many ...


2

If I understand you correctly you just want to display a different amount of posts at archives, for this you don't need a custom secondary query at all. You can control it via pre_get_posts hook, either making use of posts_per_page or posts_per_archive_page parameter. Example for pre_get_posts function wpse188786_different_number_of_posts_for_archive_ppp( ...


0

Change your query code to this: add_filter( 'post_limits', 'my_post_limits' ); // The Query $wp_query = new WP_Query( $args ); remove_filter( 'post_limits', 'my_post_limits' ); In your theme/functions.php, add this: function my_post_limits( $limit, $query ) { return 'LIMIT 0, 30'; }


0

The answer is to specify all the args on the add_filter() call, and then you can have both the array of posts and the query sent to the callback. add_filter( 'posts_results', 'do_stuff', 10, 2 );


1

i'm not exactly sure the full context of what you are doing here. there are a number of possible solutions. first is to make sure you are accessing $post as you expect. in other words, make sure it is not being affected by other activity as WordPress builds the page. i don't know if $post contains the object you expect at this point in the flow -- because ...


0

To answear my question: It's a typo. `students_email` VARCHAR 850) NOT NULL, should be: `students_email` VARCHAR (50) NOT NULL, Oh the headache. When the typo is corrected it creates a new table.


1

Okay anyone wanting to achieve this, the code works. Just save the values in the custom field without quotes. e.g. red, car <?php $list = get_field( "main_attributes" ); $array = explode(',', $list); $args = array( 'meta_query' => array( 'relation' => 'OR', array( 'key' => 'style_atrributes', ...


0

please modified yoy loop to display single post which you have coded in single.php file <?php // Start the loop. while ( have_posts() ) : the_post(); /* * Include the post format-specific template for the content. If you want to * use this in a child theme, then include a file called called content-___.php ...


0

It sounds to me like you want to get the most recent term from your issue_filter taxonomy. To do that you'd want to use get_terms() with a few arguments. $t = get_terms( 'issue_filter', array( 'orderby' => 'id', 'order' => 'DESC', 'number' => 1, 'fields' => 'ids' ) ); That should get you the most recently created term ...


1

As @bonger said, let your filters accept a second argument, which will be the instance of WP_Query that's executing the filter, and check it is indeed the main query for an event: function single_posts_fields( $sql, $wp_query ) { if ( $wp_query->is_main_query() && $wp_query->is_singular( 'event' ) ) { // Do your stuff } } ...


0

You should be able to get that number from the same $wp_query object: global $wp_query; $num_pages = $wp_query->found_posts; $num_pages will contain the total count that matches your category (or whatever other criteria you're using). If you're just looking to get a string you can display, this modified version of the function will work. function ...


0

It looks like you're calling preparePDF( $wp_query ) inside your callback, but $wp_query isn't defined and the global $wp_query shouldn't be accessible there without global $wp_query. Try instead preparePDF( $query ) and use wp_reset_postsdata() to restore the global $post object. Another important thing is that you must run your preparePDF() function ...


1

The proper argument to your tax query is terms, not term. You can see this in one of the examples in the Codex: $args = array( 'post_type' => 'post', 'tax_query' => array( array( 'taxonomy' => 'people', 'field' => 'slug', 'terms' => 'bob', ), ), ); $query = new WP_Query( ...


3

Here is a concept that you can try out to exclude 5 posts per category each from category 3 and 5 where posts are excluded that belongs to category 9 and either of the before mentioned categories. Here is the function: (CAVEAT: Requires PHP 5.4+ as I used new short array syntax. Change as needed) /** * function get_sticky_category_posts() * * Get an ...


0

Figured it out! The culprit was the plugin "Add Categories to Pages" which one of my editors had added to the site. In addition to registering post_tag and category for the page post type, it also modified the archive.php and tag.php queries to only look in the post and page types---yet another solution for the issue of including posts, pages, and ...


3

Here is another approach with pre_get_posts. Use get_posts() ( x2 queries ) to get all the post ids of each post type according to its requirements. Add the fields parameter ('fields' => 'ids') to your query arguments to just fetch post ids as this is what we need, nothing else. This will drastically improve performance and will not be that tough on ...


3

You have a syntax-ish error, or a spelling one. There is a space after "taxonomy" in your tax_query array. If that were the sum of it I'd have just posted a comment, but additionally that $query->tax_query->queries[] = $workspace_taxonomy_query; line is pointless. Nothing changes with or without it. Don't try to hack the array like that. Just use ...


1

Use the property current_post of a WP_Query object - it'll be zero for the first post: if ( $wp_query->current_post === 0 ) // First post in main loop if ( $get_videos->current_post === 0 ) // First video in current videos loop


3

Is this possible via query args? I don't think so. Is this possible via 'posts_where'? It is probably doable using some 'posts_*' filters, not only with 'posts_where'. Or maybe using 'posts_request' filter to completely oveeride the query. Do I need to run a separate query and then somehow merge the results? That would be the simplest ...


0

This is going to be a bit crappy to achieve without any potholes. The problems that you are going to face are If you are going to paginate one set of posts, the other two will also paginate regardless You are going to hit some kind of failure once you run out of posts on any of the three loops while paging through one of the other loops which have more ...


1

The WP_Query documentation on the codex specifically says: 'attachment' - an attachment. Whilst the default WP_Query post_status is 'publish', attachments have a default post_status of 'inherit'. This means no attachments will be returned unless you also explicitly set post_status to 'inherit' or 'any'. I am not sure why it was working before ...


1

You need to use a meta_query to query custom fields. If you are going to store date and time in custom fields for the purpose of ordering or comparisons, you will need to store it in the format of yyyy-mm-dd h:m:s or as a unix timestamp. If you have two custom fields start and end and you need to get posts between a cetain start and end date, you can try ...


1

What the author of the article posted, as I read it, is suggesting you do is something like this: $cats = array(1,2,3,4); $q = new WP_Query( array( 'category__not_in' => $cats ) ); var_dump($q->request); You should see the complex default query in that dump. $ids = array_unique(get_objects_in_term($cats,'category')); var_dump($ids); Now ...


3

You might try this kind of pre_get_posts hook approach (untested): add_action( 'pre_get_posts', function( $q ) { $exclude_cats = [ 12, 34 ]; // <-- Edit these category ids! if( ! is_admin() && $q->is_main_query() && ! is_singular() && ! is_category( $exclude_cats ) ) { ...


0

I am only guessing, but sollution must be similar to $args = array( 'tax_query' => array( array( 'taxonomy' => 'category', 'field' => 'id', 'terms' => array(1, 2), 'operator' => 'AND' ) ) ); $results = new WP_Query( $args );


2

This is very unusual case, and the only solution I can think of here is to get an array of category ids to include into category__in in order to skip posts that are only assigned to category 1 In order to achieve this, we must use get_terms() to get all the categories. We will use the exclude parameter to exclude category 1 from that list. We will also ...


0

I updated my question with a partial answer to this problem. By adding an underscore '_' before the name of the meta field it allows you to add meta data without it creating an editable custom field on the page edit screen. update_post_meta( get_queried_object()->ID, '_term_id', $meta_term_id);


0

You need to use a meta query: if ( isset( $_REQUEST['src'] ) ) { $query = array( 'post_type' => 'post', 'meta_query' => array( ), ); if ( isset( $_REQUEST['srcbusiness'] ) ) { $query['category_name'] = wp_unslash( $_REQUEST['srcbusiness'] ); } $fields = array( 'srctype', ...


0

You can use get_page_by_title to match the title of the required post. $post_name = get_page_by_title( 'Post Title' ); $vars = array( 'name' => $post_name, 'paged' => $page, 'posts_per_page' => intval($posts), 'order' => 'asc', 'offset' => ($offset ? $offset : ''), 'orderby' => 'title' );


2

To narrow down the possible causes I'd strongly recommend using a debugging plugin on your development server, like Query Monitor, so you can see the SQL being generated by your query. Alternatively check the request property of your $wp_query object. It's always possible, if you only recently installed the plugin, that there aren't any posts with that post ...


-1

Well if i need to use Pagination i will go with WP-PageNavi plugin. $paged = get_query_var('paged') ? get_query_var('paged') : 1; $args = array('post_type' => 'post', 'posts_per_page' => 5, 'paged' => $paged); $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); // Your Custom Loop Code Here endwhile; ?> ...


0

According to the codex If the pagination is broken on a static front page you have to add the "paged" parameter this way: if ( get_query_var('paged') ) { $paged = get_query_var('paged'); } elseif ( get_query_var('page') ) { $paged = get_query_var('page'); } else { $paged = 1; } query_posts('posts_per_page=3&paged=' . $paged); Page parameter is ...


2

I'm not quite sure what do you mean by you have extended the visual composer, but I do have a couple of concerns here. SoC -> From your code I detect that you are using a shortcode, which should have its own separate class. You should not extend the class to the visual editor (at least this is how I read your question). Classes should never multi task, ...


1

On a stock WordPress installation, WP_Query will default to post_type = 'post'. Consider this simple example: $args = array( "posts_per_page"=>5, "ignore_sticky_posts"=>true ); $itposts = new WP_Query( $args ); It will result in the following query to be executed: SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND ...


2

Building on other answers before me, to provide flexibility in the situation where you want to search a post that contains a word in a meta field OR in the title of the post, I give that option via the argument "title_filter_relation." In this implementation, I only allow for "OR" or "AND" inputs with a default of "AND." function title_filter($where, ...


0

As I already stated in comments, you should never use query_posts, only use query_posts if you are intended to break your page functionalities. Add query_posts to the very top of your EVIL LIST. query_posts breaks the main query object, the very object that your pagination function relies on. Your pagination function relies on the $max_num_pages property ...


1

change your args as bellow $args = array( 'post_type' => 'your_post_type', 'orderby' => 'meta_value', 'meta_query' => array(array('key' => 'releaseDate')), 'order' => 'ASC', 'paged' =>$paged, ); If you get the results ordered other way around, change 'order' => 'DESC', You'll get the correct order.


0

When you created your custom post type, did you also add your categories to your new post type? register_taxonomy_for_object_type( 'category', 'mycustomtype' );


0

Best solution is make changes on your php.ini Login to your cPanel or host panel Go to the File Manager. Select the Web root (public_html/www) directory and click Go. Find the php.ini file in the public_html. Note! If you do not have a php.ini in your public_html files, you can have our tech support staff restore the php.ini to your public_html ...


1

You can store the counts as key and link a value and sort them before display like this : $sort_tab = array(); while (have_posts()) : the_post(); if ( has_post_thumbnail() ) { the_post_thumbnail( array(40,40) ); } $url = get_the_permalink(); $json = file_get_contents( ...


1

As I already stated in a comment, You need to use get_option( 'posts_per_page' ) and get_query_var( 'paged' ) to calculate your post numbers You can do something like the following: (I have commented the code to make it easy to follow. This goes into functions.php) function get_post_number() { global $wp_query; /* * Get current page ...


1

UPDATE: Ok this should work, according to ACF's documentation: $args = array( 'posts_per_page' => -1, 'post_type' => 'page', 'meta_query' => array( array( 'key' => 'feature_on_front_page', 'value' => 'featured', 'compare' => 'LIKE' ), ) ); Your ...


-1

Try "posts_per_page" in place of "numberposts". numberposts is not a valid parameter of WP_Query. if you are using "ACF", then I think this will not work with WP_Query. This is not logical but I had same kind of bad experience. Try once



Top 50 recent answers are included