Tag Info

New answers tagged

1

I generally merge their ID arrays and make a third query. To keep first set of queries cheap I only return their ID's using fields parameter like this: //setup your queries with extra parameter fields => ids $query1 = new WP_Query(array('fields' => 'ids','other_parameters' => 'etc')); $query2 = new WP_Query(array('fields' => ...


1

Don't forget that when you are adding a filter it will stay there for all subsequent request. I would suggest you remove the closure and do something like this: function x_example( $input ) { remove_filter( 'posts_where', 'x_example' ); return $input; } add_filter( 'posts_where', 'x_example' ); You will have to do the same thing for the posts_join ...


0

use % to set the template(style) or you can use :nth-child(16n+1) ... <?php if (have_posts()) : ?> <?php $count = 0; ?> <?php $st = 16; //after how many blocks you want to repeat the pattern <?php while (have_posts()) : the_post(); ?> <?php $count++; ?> <?php if ($count%$st == 1) : ?> <div class="style-1"><?php ...


1

you CANT have a PAGE and a CUSTOM POST TYPE with the same name Maybe tree years ago it was impossible, but now you CAN. First, add this lines to $args into your post type: 'has_archive' => false, 'rewrite' => array( 'slug' => 'your slug', // if you need slug 'with_front' => false, ...


0

I think you need a filter on posts_orderby to do this: add_filter( 'posts_orderby', function ( $orderby, $query ) { if ( $query->get( 'orderby' ) == 'zuk' ) { global $wpdb; //$field = $wpdb->posts . '.post_title'; $field = $wpdb->postmeta . '.meta_value'; $order = $query->get( 'order' ); $orderby = ...


0

Ok, thank you all for your good answers. Anyway, I need more flexibility, so I ended up not using the wp template auto-sugars... This answer is therefore not strictly related to my own original question, but I think it can help people too. I built a custom function that retrieves a set of custom posts form a specific custom taxonomy and an optional parent ...


2

Don't concatenate your id values into a comma separate string otherwise you will have to explode() by comma delimiter which is pointless, instead, if you are getting IDs from some other business logic as evidenced in your iteration over $rows, then build an array of IDs as per the following example: $ids = array(); foreach ($rows as $obj) { $ids[] = ...


2

The p (post) paremeter for WP_Query accetps a single value only: $args = array( 'p' => 100 ); For multiple posts use post__in and pass an array of post ids: $args = array( 'post__in' => array(100, 102, 105) ); See Post & Page Paremters for further clarification.


0

If your naming system is consistent then simply ordering by the post name should do it: $args = array( 'orderby' => 'title', 'order' => 'ASC', ); $q = new WP_Query($args); var_dump($q->request); var_dump(wp_list_pluck($q->posts,'post_title')); Or, as a pre_get_posts filter (assuming the main query is what we want to alter): ...


1

Just run a normal tax_query and set the field parameter to slug in the query. I assume that you already have a way getting the array of slugs (Requires PHP5.4+) $args = [ 'post_type' => 'product', 'posts_per_page' => 12, 'tax_query' => [ [ 'taxonomy' => 'TAXONOMY_NAME', 'field' => ...


3

Always great if someone wants to use better, more correct and more efficient ways to get things done. To achieve what you need is not difficult. WORKSFLOW: Use the usort() to create your custom sort order Use the the_posts filter to sort and return the sorted array of posts just before the loop is executed Inside the loop, all you need to do is to ...


0

I used your first snippet of code and it helped me out a lot with the Sahifa Theme. Like you, my nav was disappearing on the custom post pages. After analyzing the code I realized that the following line: $post_type = get_query_var('post_type'); Was not including the other standard posts & pages when it was on the post_type pages. So I changed it ...


2

If I understand the question correctly, you want to produce something like this: Sub-Item 1 Post 1 Post 2 [...] Sub Item 2 Post 1 Post 2 [...] There may be a more efficient way than what I am about to tell you, but I would try something like this: First create an array to hold your sorted posts. $sortedPosts = array(); Next, create an array for ...


1

You have one or two issues here and possible future bugs that you might encounter term in your tax_query should be terms Be carefull with terms having dates as names and especially dates as slugs. This can cause issues and unexpected results if custom permalinks clash or if something falls into the template hierarchy for some reason. Never use the name ...


0

your cron must be register at the plugin or theme activation. Try wp crontol plugin to verify and run your cron task. But, i think you call "minutes" interval where you might call "every_three_minutes" interval for the schedule event.


2

You don't need to worry to much about your query as there is not much you can do about it. You can unfortunately not just only query categies, post titles and images natively using WP_Query. Post categories needs to be called separately inside the loop, just like featured images. If you dig deeper into core, you will see that all postdata and post terms are ...


1

As already hinted by @Milo, check if you have terms before appending your tax_query You can try the following: (Requires PHP 5.4+ due to short array syntax, revert to old syntax if necessary) $args = [ 'post_type' => 'product', 'posts_per_page' => 15, 'paged' => $paged, 'post__not_in' => $exclude, 's' => $filter, ]; // ...


0

First you can to create a schedule for one minute, then bind function with that hook. function add_new_intervals($schedules) { // add weekly and monthly intervals $schedules['every_single_minutes'] = array( 'interval' => 60, 'display' => __('Every Minute') ); return $schedules; } add_filter( 'cron_schedules', ...


1

I notice that to link to each post, I am calling get_permalink() and this is creating an extra DB query for each post. Not true. If you check the link to the very old trac ticket #18822 in the post that you have linked to, this issue was raised by @kaiser in 2011. The question was answered by @scribu For example: $posts = get_posts(); foreach ( ...


1

I don't suspect that your tax_query is failing you, but actually your tag parameter is. single_tag_title() returns the name of the tag, not the slug. All tag and category queries are converted to a tax_query in the WP_Query class before being passed to the WP_Tax_Query class to build the relative string for the SQL query. Term names and slugs gets ...


1

You have a lot of issues here: You should not be using $_GET variables without sanitizing it. Never ever trust any user inputs and URL parameters. They might contain malicious code which can harm your site. To sanitize and validate a $_GET variable, use the filter_input php function $filtered = filter_input( INPUT_GET, 'type', FILTER_SANITIZE_STRING ) ...


0

As explained in Codex post__not_in (array) - use post ids. Specify post NOT to retrieve. post__not_in argument have to be passed as array. Change your query to: $the_query = new WP_Query( array( 'category__in' => $project_category, 'posts_per_page' => -1, 'post__not_in' => array( $postid ), ) ); And it should work.


2

Do not run custom queries in place of the main query on the home page and any type of archive page. This will always cause an issue. If you need to alter the main query, use pre_get_posts to do so. To solve this issue, you need remove your code from your category page and go back to the default loop. You should only have the following in your category page ...


-1

You can also try below code to retrieve count of rows. $myquery = $wpdb->get_results( "SELECT * FROM tablename WHERE ID='some-value'" ); echo $wpdb->num_rows; Let me know if you have any problem. cheers


0

My approach was also correct but the only problem was we were missing is that resetting the query before the post navigation links. Here is the updated answer. <?php $user_fav = get_user_favorites($user_id); $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; $args = array( 'posts_per_page' => 4, ...


0

You may want to try adding the 'RELATION' => parameter into your meta query array so that you can encompass all of your meta values. Something like this: $v_args = array( 'post_type' => 'loads_available', 's' => "Post Name", 'meta_query' => array( 'relation' => 'AND', array( 'key' ...


1

Harman, don't use get_posts() when pagination is required. You may also accomplish this task using WP_Query. Refer to this answer explained beautifully by Chip Bennett at How to fix pagination for custom loops? <?php $user_fav = get_user_favorites($user_id); $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; $args ...


2

<?php $args = array( 'posts_per_page' => -1, 'post_type' => 'event', 'meta_query' => array( array( 'key' => 'event_date_end_timestamp', 'value' => time(), 'type' => 'numeric', 'compare' => '<' ) ) ); $programs = new WP_Query($args); ?> ...


0

The problem You must remember to call the core function wp_reset_postdata(), after your while loop, to restore the global $post object. The comment form is relying on that object, that you override with your $queryPosts->the_post() call. Note that the extract() isn't recommended, check this answer by @toscho, for example. Removing comments To remove ...


2

You need to set ignore_sticky_posts to true in your query arguments. This way you exclude sticky posts and only focus on the post ID's array being passed to post_in 'ignore_sticky_posts' => true, EDIT If this does not make much sense, please see my answer here to similar question where I have explained it a bit better. Be sure to check it out THE ...


0

What you are doing is right except that you are careless or you just copied code from somewhere else and pasted in without modifying it. <?php $sticky = get_option('sticky_posts'); if (empty($sticky)) { return; } $counter = 1; $posts = new WP_Query(array( 'posts_per_page' => 5, 'post__in' => $sticky, 'meta_query' => ...


1

The sticky posts are prepended on the first paginated part of the home page, so try this: add_action( 'pre_get_posts', function( $q ) { if ( $q->is_home() && $q->is_main_query() && $q->get( 'paged' ) > 1 ) $q->set( 'post__not_in', get_option( 'sticky_posts' ) ); } ); to remove it from the other paginated parts ...


1

One of two things is happening that I can think of: You have sticky posts showing up. Try adding an ignore_stickie_posts argument. $args = array ( 'pagination' => true, 'posts_per_page' => 5, 'ignore_stickie_posts' => true, ); See: http://wordpress.stackexchange.com/a/85658/21376 You have a poorly written (too global) ...


0

you have to use the serialize function in case you are using integers: $args = array( 'meta_query' => array( array( 'key' => 'my_meta_key', 'value' => serialize(strval($my_vale)), 'compare' => 'LIKE' ) ) ); $posts = get_posts( $args ); and you are ready, print_r($posts); and see ...


1

Did you try without $query-> in front of get_template_part() ?


0

Try this, it may help: if( !defined( 'ABSPATH' ) ) exit; function modify_wp_search_where( $where ) { if( is_search() ) { global $wpdb, $wp; $where = preg_replace( "/($wpdb->posts.post_title (LIKE '%{$wp->query_vars['s']}%'))/i", "$0 OR ( $wpdb->postmeta.meta_value LIKE '%{$wp->query_vars['s']}%' ...


0

before (string/array) - Date to retrieve posts before. Accepts strtotime()-compatible string, or array of 'year', 'month', 'day' values: I would try the before parameter with a value of 1 day or 24 hours


2

As birgire said date query is an array of arrays May be this solves your problemo. $args = array( 'post_type' => 'surf_reports', 'posts_per_page' => '1', 'category_name' => $cat (this is pulled dynamically in my template), 'date_query' => array( array( 'after' => '24 hours ago' ) ) );


0

Using $wp_query as your variable is a bad idea as it is something WordPress uses itself. For returning as many results as you would like you can use posts_per_page in your query. There are many more parameters you can query with in WordPress. $custom_wp_query = new WP_Query('posts_per_page=10&post_type=events');


1

Create a template with a WP_Query to get posts with post type "post" then create a page and assign that template to that page. Templates: https://codex.wordpress.org/Templates Query by post type: https://codex.wordpress.org/Class_Reference/WP_Query#Type_Parameters


0

I think do you mean GROUP Post by category/taxonomy NOT SORT. Here, Is a code to GROUP by category/taxonomy $terms = get_terms( 'my_cat_name' ); Here, cat_name name is the name of taxonomy, When you register it like this: e.g. register_taxonomy( 'my_cat_name', array( 'custom_post_name' ), $args ); Use it in Query e.g.: $args = array( ...


0

You are using the global $wp_query not your custom query. Either declare yours global too like global $wp_query_archives and then use that in the sm_inline_post_class callback while being aware of the drawbacks of using global variables or do a little work like so: $class = sm_inline_post_class( array(), $my_custom_query ); And then pass $class as the ...


1

No there is no issue you can use query inside a loop with the help of reset post data and query wp_reset_query() - ensure that the main query has been reset to the original main query wp_reset_postdata() - ensures that the global $post has been restored to the current post in the main query.


1

I tested the query and it does work so the only thing I can spot that is misleading but not right is directly after you while() statement you have the_post(). This doesn't work in secondary queries, it should look like: <?php if( $subpages->have_posts() ) : ?> <?php while( $subpages->have_posts() ) : $subpages->the_post(); ?> ...


0

If you're saving an array into Metadata it becomes Serialized. This makes it pretty difficult to query on, maybe try using wildcard: $args = array( 'ignore_sticky_posts'=> 1, 'post_type' => 'post_projects', 'meta_query' => array( array( 'key' => 'wpl_location', 'value' => '%Europe%', ...


0

You might be missing the 'meta_key' arg here: $args = array( 'ignore_sticky_posts'=> 1, 'meta_key' => 'wpl_location', // here 'post_type' => 'post_projects', 'meta_query' => array( array( 'key' => 'wpl_location', 'value' => 'Europe', 'compare' => 'IN', ), ), ...


0

The trick is to call the hook "wp_insert_post". But the post-type is not what you expect - post, page oder custom post type. It's "revision". Now you can fire a WP_Query right after the post is saved in the database.


-1

You could add to your functions.php (in child theme) a reference to just that one page (is_page('law-practices')) and enqueue just for that one page: add_action( 'wp_enqueue_scripts', 'law_practice_enqueue_style' ); function law_practice_enqueue_style(){ if (is_page('law-practices')){ wp_register_style( 'law-styles', get_template_directory_uri() . ...


0

Add the CSS to your child themes style.css file or load it using wp_enqueue_scripts add_action( 'wp_enqueue_scripts', 'wpsites_load_custom_styles' ); function wpsites_load_custom_styles() { wp_register_style( 'custom-css', get_stylesheet_directory_uri() . '/your-style.css', false, '1.0.0' ); wp_enqueue_style( 'custom-css' ); } Source


-2

Please try this instead. Replace your css by : <link rel="stylesheet" type="text/css" href="<?php echo get_template_directory_uri(); ?>/assets/stylesheets/styles.css" /> in the head section



Top 50 recent answers are included