Tag Info

New answers tagged

3

Yes, it is possible, but it is a bit of a pain. Looking at the codex page, only arguments of note are number and offset. We need these two to create our paginated pages. First, we set the $paged parameter, which is the current page: $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; Then number of comments to display: $number = 3; ...


0

I also run into this situation. Here what i did: $args = array( 'post_type' => 'my-post-type', 'meta_query' => array( array( 'key' => 'latitude', 'value' => sprintf(':"%s";', $value), 'compare' => 'LIKE' ) ) ); Hope this help


1

I'm not quite sure what you want to do as frankly, your terminology is a bit scrambled, and you example shortcode is also quite a mess. Before I start, you should start of by checking the following very important links Shortcode API WP_Query You should first start of by defining your attributes (and their defaults) which you need to use and pass to the ...


0

EDIT: Well, it seems the following approach is a bit DB consuming. Pieter Goosen posted a very interesting alternative. Since this approach has already been accepted as a correct answer I don't know, it it is possible to mark this question still as a duplicate. I flagged my answer so a moderator might have a look. Thanks. I first collect all data in an ...


2

comments_number() echo's the number of comments, while get_comments_number() returns the number of comments as a string, so what you are likely looking for is get_comments_number instead of comments_number, like echo '<ul>'; while ( $query->have_posts() ) { $query->the_post(); $title = get_the_title(); echo '<li>' . $title . ...


-1

I use this: add_action('pre_get_posts','search_filterr');function search_filterr($query) { if ( !is_admin() && $query->is_main_query() ) { if ( $query->is_search ) { $arrs[]='post'; $query->set('post_type', $arrs ); query->set('category__in', $CAT_ID); } ...


0

Found the problem. In my args, I should have included this line: 'post_type' => array( 'one', 'two', 'three', 'four', 'five', 'six' ), above the 'tag' arguments


0

The main problem is that, in your $_POST loop, $value is an array, yet you apply htmlspecialchars to it, which will vomit and return back nothing. If you haven't done so already, set WP_DEBUG to true in your wp-config.php - developing without it is simply not an option. Regardless, let's fix that dirty $_POST loop: $tax_query = array(); // Don't need ...


0

This is strange and should not happen by default. get_posts uses WP_Query. If you look at the source code of get_posts, all the parameters passed to get_posts is passed unchanged to WP_Query except parameters like category which is changed to cat, the include and exclude parameters to include or exclude certain posts which is changed to post__in and ...


1

It's not related to WordPress at all, it's a generic PHP question, hence off-topic here. It's easy, follow the inline comments, and you can have it. :) <?php //declare a counter $counter = 1; // 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 ...


1

To expand on @pieter-goosen's comment, you do indeed want to use pre_get_posts. In your example, by using WP_Query, you're overwriting the entire query and just resetting most parts of it to default. In fact, you're probably not seeing a specific category of posts at all. You should be seeing all posts since that's the default of the WP_Query class. So ...


0

Outside of subqueries, MySQL will only process 1 query at a time. When running sql statements through phpmyadmin, it just executes 1 query after the other. To do the same thing in WordPress, it appears you just use 1 query per $wpdb object (those can have subqueries if desired, but not what's needed here).


1

For anyone looking for this answer in the future, I took woony's code the extra mile and got everything working within Wordpress' post_meta table structure. This assumes you have two separate custom fields, one for Latitude (city_latitude) and one for longitude (city_longitude). Just pass the latitude, longitude, and distance parameters into a WP function ...


1

pagename is for request page (core page post type) by slug. What you want, if I understood correctly, is to get "attorney" posts that belongs to same case-log terms that current "case" post. I would do it something like this (not sure where you are going to execute the code, I've tried to code as universally as possible): if( is_singular( "case" ) ) { ...


1

You're right, RAND() is a performance killer which almost every time involves a full table scan, and if you have a large number of posts in WordPress, it's going to be a nightmare. So instead of displaying random posts, it's much more efficient to display what only seems like a random post, but in reality is not. Here's a simple example: You have the ...


0

I go the solution. I have take variable name like $wp_query = new WP_Query($arr); instate of $the_query = new WP_Query($arr); So my working code should be <div id="primary" class="content-area"> <main id="main" class="site-main" role="main"> <?php // global $wp_query; $paged = (get_query_var('paged')) ? ...


0

Use wp_parse_id_list to split a comma separated string of IDs into an array: 'post__in' => wp_parse_id_list( $post_id ),


0

Have you tried adding wildcard symbols to the array of options in the meta_query? Since the meta value is saved as a serialized array, by default this query is trying to exactly match the value - as in, it will only work if meta_value equals EXACTLY either "single" or "redundant". Since in your case it's serialized, it won't work. I haven't tested this so ...


0

Submitting a full blown answer now that we've figured it out. Transients can be hard to troubleshoot because if you save the wrong value to the database at first, you can be looking at an old—and bad—value, even if the code that sets your transient is now correct. The two ways you can fix this are to temporarily use delete_transient() or use a plugin ...


0

You are almost there, just add a meta_key and meta_value to your arguments. $args = array( 'meta_key' => 'ss_af_category', 'meta_value' => true, //Rest of arguments ); Also note, posts_per_page is the correct parameter to use, showposts is depreciated


0

Assuming that the query works and you get all posts with ss_category terms (instead of those with ss_aff_category = true only), I think this should work: $args = array( 'post_type' => array('advertisers_cats'), 'posts_per_page' => 9, 'tax_query' => array( array( 'taxonomy' => 'ss_category', 'terms' ...


0

Figured out a solution. Ended up querying each categories using "foreach" and inserting my custom function into the loop. Not sure if it's the best approach but I got it to work so eh. No one else seems to want to help.


1

I am not sure without running which query precisely this results in, but I don't think it is intended format for such query. If you take a look at Date Parameters documentation, there is a special case of after and before when you want to find posts in some direction from specific point.


1

I'm currently busy with an extensive navigation plugin for single posts and pages which I hope to release in future which will do the same exact thing which you are after. The integral part is using a date_query in a custom WP_Query to fetch the adjacent posts according to the parameters passed to it. Here is an idea coming from the plugin To accomplish ...


1

The correct syntax to get posts from the last week is 1 week ago. Just another note, showposts is depreciate in favor of posts_per_page. Apart from that, your query should work.


0

I was having the exact same problem while querying post and pages, but also the jetpack-portfolio post type in my custom widgets. I was using $query = new WP_Query( $query_args ); in both cases. For me the fix was to change $query to $query_entries and $query_projects. It works as expected now. I see you're already using $products, but maybe it conflicts ...


0

The first issue I'm seeing is that you're trying to use the_category as a class. This won't work as this function is intended to output the links to the categories assigned to that post. So it's putting the href inside your class. You can debug and see how it works by removing your li and the class. Then see how the_category works by itself. You can inspect ...


1

Based on the answer by @jan-becker I build this snippet. This works for me: /* * Get the next/prev image id inside c-tax: my_snapshot_position_ctax */ function my_return_relative_attachment_id( $this_post_ID, $prev = true ) { global $post; // overwrite global $post variable $post = get_post( $this_post_ID ); // filter sql query to work ...


0

I solved my problem by adding a hook on the action wp_insert_comment. When my hook is called i calculate the average rating for each rating meta-tag of my comments. Then i add a meta tag to the post with the result I can use the post meta-tag to sort with wp-query


1

You can use $_GET to get the query string values and then pass it in the $args to create a dynamic WP_Query $after = $_GET['after']; $before = $_GET['before']; and then in the $args if ($after && $before) { $args = array( 'date_query' => array( array( 'after' => $after, 'before' ...


0

Try this out <?php /** * Get next/preview post by post_date * * @param int|WP_Post $post * @param boolean $next_post * @return WP_Post|WP_Error */ function get_next_prev_post_by_date( $post, $next_post = true ) { global $wpdb; // get current post data if ( !is_a( $post, 'WP_Post' ) ) $post = get_post( $post ); // ...


0

The get_adjacent_post function already does what you are asking for. Its only downside is that it uses the global $post variable to determine the current post. You would need to overwrite it in a wrapper function like so: <?php function get_adjacent_post_by_id( $post_id, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = ...


0

You could use the function wp_get_nav_menu_items() instead of wp_query() to retrieve the menu items from a particular menu in the menu structure order. Once you've done that, you could loop through the menu items one by one to do with as you wish (including filtering them down further on type or other info). This is assuming all pages are in the menu (which ...


1

You may be able to use a WP_Query on nav_menu_item since it is its own post type. I've never done this but maybe it would work like you need it to, worth a shot. There are three other possibilities: Option 1 - Get Your Nav Menu Items There's a functions called wp_get_nav_menu_items() which will return you an array of your menu items that you can then loop ...


2

I agree with Rarst about the WEEK function, but be sure to also use YEAR as well to order the posts. So your order by would look something like this: ORDER BY YEAR($wpdb->posts.post_date) DESC, WEEK($wpdb->posts.post_date) DESC, $wpdb->postmeta.meta_value+0 DESC Of course, this is still pulling all posts, so you might also want to just limit the ...


1

This is more of an MySQL question than WordPress one. You are already using date functions there, from a quick check there is (unsurprisingly) one for WEEK(date[,mode]), you could use in similar fashion for order.


0

OK, I have made a basic error here. I have hooked into parse_query and made the array blank if certain paramenters are met, which they where here. Thanks for your help guys, you led me to where to look.


0

This works better for me overriding both blog posts and static page in Settings > Reading > Front page displays: <?php /** * Set custom post type archive as front page. * * @since 1.0.0 */ function ql_set_as_front_page( $query ) { if ( is_admin() || ! $query->is_main_query() ) { return; } if ( ql_is_front_page( $query ) ) { ...


3

Here's a straight up simple solution. Requires you to have the most recent version of WordPress though. (or at least 4.1) Using nested taxonomy query. Taking what you have, and just adding to it a bit. $args = array( 'post-type' => 'episode', 'post-status' => 'publish', 'posts_per_page' => 4, 'tax_query' => array( ...


0

I created a custom where clause. I tested it using $wp_query->request right before my main loop, I don't really know SQL that well, but this seemed to get things working. add_action('pre_get_posts', 'add_trending_sort', 11, 1); function add_trending_sort($query){ if(!$query->is_main_query()) return; //Overwrite query arguments ...


1

Query parsing happens on every query run on a page, not just the main query. Your parse_tax_query is modifying the query for menu items in your menu. Check is_main_query() to make sure you only change the main query: function no_child_terms($query) { if( !is_admin() && $query->is_main_query() ){ ...


0

Early optimization is the source of 99% of the unreadable and unmaintainable code. (statistics I made up just now) There are two kinds of optimizations to make things faster to make things reasonable fast You know what is the type 1 optimizations you need only after your code is ready and you can do general profiling and decide what is worth the effort ...


0

I will not worry to much about performance here, but I will optimize the code a bit by calling get_post_meta() only once. This will not increase or decrease performance as you will see in the post I have linked to later on, but it comes down to the principle of not repeating yourself. On the contrary to what you are thinking, querying custom fields is ...


2

I made a few adjustments. Once I created my two arrays, I combined them this way $merged_queries->posts = array_merge( $women->posts, $men->posts ); shuffle($merged_queries->posts); $merged_queries->post_count = $women->post_count + $men->post_count; while ( $merged_queries->have_posts() ) { $merged_queries->the_post(); // do ...


0

Your code should work in theory. Your failure is not caused by the code in your question but rather due to external factors. Something else is causing this failure DEBUGGING THIS ISSUE Turn on debug and check for obvious errors and warnings. This should be a good indication on the soundness of all your code on a particular page Clear all caches, not just ...


0

Just remove the ID then. $images = get_the_author_meta( 'images', $user->ID ); $images = explode(',',$images); $images is an Array isn't it? Then I hope this one can help you. http://stackoverflow.com/questions/2448964/php-how-to-remove-specific-element-from-an-array


0

I have checked this and it works fine. Check meta_key or check custom loop if you use it elsewhere before this and reset its post data <?php $array = array( 'post_type'=>'post', 'posts_per_page'=>4, 'order'=>'DESC', 'orderby'=>'meta_value_num', 'meta_key'=>'_count-views_all'); $the_query = new WP_Query($array); if ( ...


2

I think the best way here would be to run three separate loops here (you can also do two loops, but then you need to get full posts in the men and women queries, combine them and shuffle them), the first two will be used to get 5 men and the second loop will get 2 women. You can adjust this as necessary. The third loop will use the info from the first two ...


0

If you use those custom fields only for the company tagged posts, then it's sufficient to retrieve the custom fields without worrying about posts and tags: $tags = array('Professional Rating', 'Efficiency Rating', 'Referral Rating'); foreach ($tags as $tag) { $result = $wpdb->get_col( "SELECT meta_value FROM wp_postmeta WHERE meta_key = '$tag'" ); ...


0

Yuo can also do it this way, count published posts, get settings posts per page, then divide. $published_posts = wp_count_posts()->publish; $posts_per_page = get_option('posts_per_page'); $page_number_max = ceil($published_posts / $posts_per_page);



Top 50 recent answers are included