Fetch contact-form-7 id to another plugin - wordpress

How to fetch contact-form-7 forms ID, to another plugin??
I want to get forms id and using that id, ill give some effect to that form, please tell me how to fetch form id from contact-form-7, to another plugin in wordpress.

Actually in contact form 7,the post type is wpcf7_contact_form
So you can use this bellow code . In tis code the function return an array of all contact form's id.
function get_cf7_IDS(){
$cf7_id_array =array();
if ( post_type_exists( 'wpcf7_contact_form' ) ) {
$args = array(
'post_type' => 'wpcf7_contact_form',
'posts_per_page'=>-1,
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$cf7_id_array[]= get_the_ID();
}
wp_reset_postdata();
}
}
return $cf7_id_array; //RETURN THE ARRAY OF IDS
}
Then use this function to get all ides in array get_cf7_IDS().
Then let me know the result.
Thanks

Try below code. i hop it help you
$args = array('post_type' => 'wpcf7_contact_form', 'posts_per_page' => -1);
$cf7Forms = get_posts( $args );
foreach($cf7Forms as $f)
{
// Contact form id
echo $f->ID;
}

Related

How to Enable Ascending or Descending In WordPress Default Built-in Loop

Can someone please let me know how we can add Order functionality to WordPress Built-in Loop?
<?php
if (have_posts()) {
while (have_posts()) {
the_post();
// Post Content here
} // end while
} // end if
?>
I know this is doable in WP_Query like
$args = array(
'orderby' => 'title',
'order' => 'DESC',
);
$query = new WP_Query( $args );
but how we can do this is default loop?
You will have to add a filter for the loop separately in functions.php.
function my_loop_filter( $query ) {
if ( $query->is_home() && $query->is_main_query() ) { // Run only on the homepage
$query->query_vars['orderby'] = 'title';
$query->query_vars['order'] = 'DESC';
}
}
add_action( ‘pre_get_posts’, ‘my_loop_filter’ );

Display Job Post of individual user id in WP JOB manager

I m trying to display Job listing of individual user based on User ID in his respective account as "My Listing".
I checked there forum still didn't find any solution.
You need a custom query and to include the current user id. Something like this:
$user = wp_get_current_user();
$args = array(
'author' => $user->ID,
'post_type' => 'job_listing',
);
$query = new WP_Query( $args );
$query->the_post();
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
//Loop Content Here
}
}

Wordpress batch for move posts status for custom type

Is there any way for change all custom posts type status change from "publish" to Draft ?
Scenario:
Here I need to run a script.
my post type is: property_listing
Now I need to make all post draft then perform my custom operation.
Is it possible ?
If you have Cpanel or database access you can run this query in DB panel.
update table wp_posts set status='draft' where post_type='property_listing'
otherwise, use this code.( paste it in your theme function.php)
add_action('init','change_mypost_status');
function change_mypost_status(){
$args = array(
'post_type' => 'property_listing',
'posts_per_page' => -1
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$current_post = array(
'ID' => get_the_ID(),
'post_status' => 'draft'
);
// Update the post into the database
wp_update_post( $current_post );
}
wp_reset_postdata();
}
}

$wp_query and WP_QUERY - same arguments, different results

I spend this whole day trying to figure out a problem with a combination of a custom query and custom post types. This is my last resort...
The setting
I wrote a plugin that introduces some custom post types to my WordPress. To display them in the main query I hooked them into the query like this:
function add_cpt_to_query( $query ) {
*some code...*
// add custom post types to query
if ( !( is_admin() || is_post_type_archive() || is_page() ) && $query->is_main_query() ) {
$query->set('post_type', array_merge( array('post'), $cpt ) );
}
}
add_action('pre_get_posts','add_cpt_to_query');
In my theme on the other hand I setup ajax pagination like this:
function setup_pagination() {
global $wp_query;
$max_pages = $wp_query->max_num_pages;
$current_page = ( $wp_query->paged > 1 ) ? $wp_query->paged : 1;
$ajaxurl = admin_url( 'admin-ajax.php' );
wp_register_script( 'ajax-pagination', get_template_directory_uri() .'/js/dummy.js', array('jquery'), '', true);
wp_localize_script( 'ajax-pagination', 'ajaxpagination', array(
'max_pages' => $max_pages,
'current_page' => $current_page,
'ajaxurl' => $ajaxurl,
'query_vars' => $wp_query->query_vars
));
wp_enqueue_script( 'ajax-pagination' );
}
add_action( 'wp_enqueue_scripts', 'setup_pagination' );
function pagination() {
$query = $_POST['query_vars'];
$query['paged'] = $_POST['next_page'];
/*
$query = array(
'paged' => 2,
'post_type' => array('post', 'custom_post_type_1', 'custom_post_type_2' )
);
*/
$posts = new WP_Query( $query );
$GLOBALS['wp_query'] = $posts;
// Start the loop.
while ( have_posts() ) : the_post();
?>
*some code...*
<?php endwhile;
die();
}
add_action( 'wp_ajax_nopriv_ajax_pagination', 'pagination' );
add_action( 'wp_ajax_ajax_pagination', 'pagination' );
and the script part:
$.ajax({
url: ajaxpagination.ajaxurl,
type: 'post',
data: {
action: 'ajax_pagination',
query_vars: ajaxpagination.query_vars,
next_page: parseInt(ajaxpagination.current_page) + 1
}
});
The problem
If I pass the query_vars array I get from $wp_query with the altered 'paged' value back to WP_QUERY, it returns the wrong set of posts. It looks like, that WP_QUERY does not account for the cpts in the loop. Though these cpts are mentioned in the 'post_type' of the query_vars array and thereby passed along to the new WP_QUERY.
When I manually set 'post_type' and only pass this argument, it works as intended. The aspect that blows my mind is, that the resulting query_vars used in the ajax call to WP_QUERY are exactly the same, but only when I manually set 'post_type' the pagination works as it should.
I dont know if this was a somewhat understandable explanation, but I'm thankful for every idea that could help me out. Big THX!
Ok... I got it now.
I made a mistake in wp_localize_script(). The query_vars should be a json-string, I on the other hand just passed the array itself. My code above has to be altered in two lines:
function mk_setup_pagination() {
...
wp_localize_script( 'ajax-pagination', 'ajaxpagination', array(
...
'query_vars' => json_encode($wp_query->query_vars) <- convert to json-string
));
...
}
function mk_pagination() {
$query = json_decode( stripslashes( $_POST['query_vars'] ) , true); <- convert json-string to array
...
Works like a charm now. :)
btw: the code is based on a tutorial by wpmudev.org: Loading WordPress Posts Dynamically With AJAX

How to refresh pagination links after submit new query?

I modified WordPress main loop for blog posts, made it query and loop through different types of posts according to what filter is submitted.
At the beginning of the HTML, there is a form with several buttons as option, it's similar to the SA site menu: Questions Tags Users Badges Unanswered.
When visitor click one of the menu button, a new WP_Query is submitted.
Below the options, is the loop of posts, depends on what filter is submitted.
The php codes is basically a standard wordpress loop with customized functions:
$args= !empty($_POST['filter']) ? $_POST['filter']:null;
get_option_nav();
if( my_loop_have_posts(array('filter'=>$args))):
while my_loop_posts(): the_my_loop_post();
get_template_part('contents');
endwhile;
endif;
Things are working fine except that only the default filter get the correct pagination links. After submit a new filter, when click the pagination links, it goes to the default filter's pages. How to modify wordpress pagination links to get it work for the other filters?
Here's the code that I use for query args:
function get_args($args){
$defaults= array(
'order' => 'DESC',
'orderby' => 'modified',
'max_num_pages' =>5,
'paged' => get_paged (),
'post_status' => 'any',
'post_type' => array ('post', 'docs','topics'),
'posts_per_page' => 5,
)
$args = wp_parse_args ( $args, $defaults );
extract ( $args );
if(!empty($args['filter'])){
switch ($args['filter']){
case 'top_voted':
$args['post_type'] = 'docs';
$args['meta_key'] = '_vote_total';
$args['order'] = 'DESC';
$args['orderby']='meta_value_num';
break;
case 'unanswered_questions':
$args['post_type] = 'topics';
$args['blabla'] = 'blabla';
break;
default:
blabla;
break;
}
return $args;
}
and the code for the paged:
function get_paged() {
global $wp_query;
if ( get_query_var( 'paged' ) ) {
$paged = get_query_var( 'paged' );
} elseif ( ! empty( $wp_query->query['paged'] ) ) {
$paged = $wp_query->query['paged'];
}
if ( ! empty( $paged ) )
return (int) $paged;
return 1;
}
When you make a custom query, you need to explicitly call every parameter of the query. Thus, standard ones disappear, as is the pagination case.
Depending on how you are making this new query you could try:
To concatenate at the beginning $query_string, for example
$query = new WP_Query( $query_string . 'order=DESC' ) As it is explained in the Codex:
If you want to preserve the original query parameter information that
was used to generate the current query, and then add or over-ride some
parameters, you can use the $query_string global variable in the call
to query_posts().
To get the pagination from the default query and use it in your custom query
$paged = get_query_var('paged'); $query = new WP_Query( array( 'paged' => $paged ) );
In your case there is something wrong with your get_paged() function. I have tested it and it does not work, but I have not found the bug. Instead, I tried what I usually use, and it works for me:
function get_paged() {
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) { $paged = get_query_var('page');
} else { $paged = 1; }
return $paged;
}
Something seems to fail in you else if statement. Let me know if this solves your issue.

Resources